sql server - TABLOCK vs TABLOCKX -


what difference between tablock , tablockx http://msdn.microsoft.com/en-us/library/ms187373.aspx states tablock shared lock while tablockx exclusive lock. first maybe index lock of sorts? , concept of sharing lock?

big difference, tablock try grab "shared" locks, , tablockx exclusive locks.

if in transaction , grab exclusive lock on table, eg:

select 1 table (tablockx)

no other processes able grab any locks on table, meaning all queries attempting talk table blocked until transaction commits.

tablock grabs shared lock, shared locks released after statement executed if transaction isolation read committed (default). if isolation level is higher, example: serializable, shared locks held until end of transaction.


shared locks are, hmmm, shared. meaning 2 transactions can both read data table @ same time if both hold s or lock on table (via tablock). however, if transaction a holds shared lock on table, transaction b not able grab exclusive lock until shared locks released. read locks compatible at msdn.


both hints cause db bypass taking more granular locks (like row or page level locks). in principle, more granular locks allow better concurrency. example, 1 transaction updating row 100 in table , row 1000, at same time 2 transactions (it gets tricky page locks, lets skip that).

in general granular locks want, may want reduce db concurrency increase performance of particular operation , eliminate chance of deadlocks.

in general not use tablock or tablockx unless absolutely needed edge case.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -