Question

I am new to SQL Server and am not sure about how the locking mechanism works here. Please help

Issue: Need to delete close to 5 mil records in a table. A website pulls data from that table quite frequently.

My understanding is: delete would lock only the row hence this shouldn't affect the website. Is that right? Then, what would happen if the website is trying to pull the record that is being deleted? I know I sound very amateurish but I badly need to understand these basics.

Could one of the gurus please share a link where I can read up about the various kinds of locks in SQL Server as well

Was it helpful?

Solution

Yes, SQL Server uses row-level locks on those rows it inserts/updates/deletes - up to a certain point.

That point is roughly 5000 rows - if you try to update or delete more than 5000 rows in a single transaction, then SQL Server will do a lock escalation and place an exclusive lock on the entire table in question (SQL Server does not lock pages - it will escalate to the entire table - or a partition, if the table is partitioned - straight from the row-level locks).

From that point on, no more even SELECT's are allowed on that table until the delete transaction has committed (or has been rolled back).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top