Saturday 20 February 2021

SQL Server: What is the purpose of ROWLOCK on Delete and when should I use it?

 

Rowlock is a query hint that should be used with caution (as is all query hints).

Omitting it will likely still result in the exact same behaviour and providing it will not guarantee that it will only use a rowlock, it is only a hint afterall. If you do not have a very in depth knowledge of lock contention chances are that the optimizer will pick the best possible locking strategy, and these things are usually best left to the database engine to decide.

ROWLOCK means that SQL will lock only the affected row, and not the entire table or the page in the table where the data is stored when performing the delete. This will only affect other people reading from the table at the same time as your delete is running.

If a table lock is used it will cause all queries to the table to wait until your delete has completed, with a row lock only selects reading the specific rows will be made to wait.

Deleting top N where N is a number of rows will most likely lock the table in any case.

 

Reference: What is the purpose of ROWLOCK on Delete and when should I use it?

No comments:

Post a Comment