Re: delete statement makes SQL Server hang

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Itzik Ben-Gan (itzik_at_REMOVETHIS.SolidQualityLearning.com)
Date: 08/25/04


Date: Wed, 25 Aug 2004 22:38:24 +0300

A DELETE statement is always fully logged, so if the table has a large
number of rows, this will take a while and also cause the following
problems:

* SQL Server limits the number of locks it allows a single transaction to
acquire. There's a formula determining that number, but its not exposed, and
it relies on several dynamic factors (e.g., free resources in the system).
But generally, a transaction won't be allowed to get more than say, several
thousands of individual row locks. Once your transaction hits this value,
the individual row locks escalate to a full table lock (in a DELETE case, an
exclusive one). Meaning that the whole table is blocked for both committed
reads and writes.
You can capture a lock escalation event with a profiler trace. Use it to
determine the lock escalation point against your table, and divide the
number you got by 2, baring in mind the dynamic nature of the formula.

* The whole process is a single transaction, meaning that the transaction
log space occupied by the transaction can not be overridden until the
transaction is committed and the transaction log is backed up. In a large
delete, this means that your log will expand, and expand, and expand, etc.

Also, if other processes hold locks on the table (either for committed reads
or for writes), your activity will be blocked and seem to hang.

Any way, the way to go with clearing a whole table is by using TRUNCATE
TABLE. This process deallocates whole extents and writes only the extent
addresses to the transaction log, so its faster in orders of magnitude than
a DELETE.
If you cannot use TRUNCATE (either because foreign keys reference the table,
or you need a partial DELETE), split your large DELETE into chunks as
follows:

SET ROWCOUNT 1000 -- or another number that doesn't cause lock escalation

WHILE 1=1
BEGIN
  DELETE ...
  IF @@rowcount < 1000 BREAK
END

SET ROWCOUNT 0

This process splits the large DELETE to multiple small DELETEs, thus
preventing lock escalation, and allowing transaction log space to be
overridden, assuming you issue backup log operations during the activity
(possibly with an automatic alert).

-- 
BG, SQL Server MVP
www.SolidQualityLearning.com
"Derek Ruesch" <anonymous@discussions.microsoft.com> wrote in message
news:000e01c48acd$f2b7d840$a501280a@phx.gbl...
> I am trying to run a SQL Delete statement on a table and
> it causes SQL Server to hang (SQL Server seems to be
> processing the statement but it doesn't do anything. No
> records are deleted from the table and SQL Server
> continues to run the Delete query.). I am only having this
> problem on one table. This table does have several users
> that have "SELECT" permissions.
>
> I am running SQL Server 2000.
>
> The statement that I am trying to run is
>
> DELETE
> FROM tblContract
> (I want to delete all records from this table.)
>
> Why isn't the Delete statment working. I have been able to
> delete records from this table before without any problems.
>
> Thanks for the help.
>
> Derek Ruesch
>
>


Relevant Pages

  • Re: Table Variable vs Temporary Table
    ... > logging to the transaction log for table variables). ... Pro SQL Server 2000 Database Design - ... >>> We have a query that declares a table variable, ...
    (microsoft.public.sqlserver.programming)
  • Re: begin and end transaction and transaction log
    ... recovery, SQL Server will regularly truncate the transaction log, and thus ... Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books ... ...
    (comp.databases.ms-sqlserver)
  • RE: How to Pause/Resume Transactional Replication
    ... is recorded in the transaction log. ... distribution database where it stays until read by the ... Paul Ibison SQL Server MVP, ...
    (microsoft.public.sqlserver.replication)
  • Re: all about transaction
    ... >these details in the transaction log right ?? ... It's a physical file and SQL Server uses no caching for this file. ... >after that he rolledback all the old data will come back and the data on ...
    (microsoft.public.sqlserver.programming)
  • Re: Transaction Isolation Level
    ... This means that while I initiate a transaction selecting all new orders it ... My query is executed on a SQL server 2005. ... the isolation level you should use is snapshot isolation. ...
    (comp.databases.ms-sqlserver)