Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serializable transaction isolation lock

I have set a serializable transaction isolation lock for a transaction.But i am observing some results which are not expected.

My query is

update tabl1 set col2 = 10 where col1 > 10 and col1 < 20

Here col1 is primary key.Here rows with col1 having value 10,11,12....19,20 are locked for update/insert. In where condition i have given conditions as col1>10 and col1<20 but still rows having col1=10 and col1=20 gets locked.

If i give

update tabl1 set col2 = 10 where col1 => 10 and col1 <= 20

then rows with col2 having values from 9 to 21 gets locked.So why col1 having 9 and 21 gets locked here?

For the below query full table gets locked.Here col3 is a non-primary column.Can't i set lock if the column in where condition is not a primary key?

update tabl1 set col2 = 10 where col3 > 10 and col3 < 20
like image 750
wincoding Avatar asked Oct 06 '22 10:10

wincoding


1 Answers

For key range locking SQL Server must use an index to satisfy the range condition so regarding your last query you would need an index on col3 (and possibly query hints to force its use if the plan does not use it)

Also it does not just lock the specific range in your WHERE clause. It locks keys. A range lock on a key protects the range from that key downwards to the next key so the exact range that will be blocked depends upon what keys exist in the index.

More details/links are in my answer here

like image 180
Martin Smith Avatar answered Oct 10 '22 10:10

Martin Smith