Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Delete SQL locks the table which prevents new data insert into SQL Server 2005?

If I try to delete a brunch of data from a table. say

DELETE FROM myTable Where CreationDate < GetDate()

that takes hours to be deleted, will this table be locked and no new insert can happened?

This table doesn't self reference itself. I would assume I can still insert new data while it is deleting. Will the delete sql uses an exclusive lock that prevents all access to the table?

Thanks

like image 991
dsum Avatar asked May 26 '11 17:05

dsum


People also ask

Does DELETE lock table SQL Server?

DELETE uses a row lock while executing, which means each row in the table is locked for deletion. Once DELETE is executed, a table can still contain empty data pages.

Does SQL Server lock table on INSERT?

When inserting a record into this table, does it lock the whole table? Not by default, but if you use the TABLOCK hint or if you're doing certain kinds of bulk load operations, then yes.

What causes table locks in SQL Server?

Locks are held on SQL Server resources, such as rows read or modified during a transaction, to prevent concurrent use of resources by different transactions. For example, if an exclusive (X) lock is held on a row within a table by a transaction, no other transaction can modify that row until the lock is released.

Does DELETE lock database?

When you execute an INSERT, UPDATE, or DELETE statement, the database server uses exclusive locks. An exclusive lock means that no other users can update or delete the item until the database server removes the lock.


1 Answers

You can batch them to prevent it from locking the whole table:

WHILE 1 = 1
  BEGIN
        DELETE  TOP 10000 FROM  myTable WHERE CreationDate < GetDate()

        IF @@ROWCOUNT = 0
            BREAK
  END
like image 198
Hugo V Avatar answered Sep 28 '22 07:09

Hugo V