Delete statement in SQL is very slow

前端 未结 15 686
自闭症患者
自闭症患者 2020-12-22 23:23

I have statements like this that are timing out:

DELETE FROM [table] WHERE [COL] IN ( \'1\', \'2\', \'6\', \'12\', \'24\', \'7\', \'3\', \'5\')
相关标签:
15条回答
  • 2020-12-23 00:02

    Is [COL] really a character field that's holding numbers, or can you get rid of the single-quotes around the values? @Alex is right that IN is slower than =, so if you can do this, you'll be better off:

    DELETE FROM [table] WHERE [COL] = '1'
    

    But better still is using numbers rather than strings to find the rows (sql likes numbers):

     DELETE FROM [table] WHERE [COL] = 1
    

    Maybe try:

     DELETE FROM [table] WHERE CAST([COL] AS INT) = 1
    

    In either event, make sure you have an index on column [COL] to speed up the table scan.

    0 讨论(0)
  • 2020-12-23 00:02

    open CMD and run this commands

    NET STOP MSSQLSERVER
    NET START MSSQLSERVER
    

    this will restart the SQL Server instance. try to run again after your delete command

    I have this command in a batch script and run it from time to time if I'm encountering problems like this. A normal PC restart will not be the same so restarting the instance is the most effective way if you are encountering some issues with your sql server.

    0 讨论(0)
  • 2020-12-23 00:03

    Things that can cause a delete to be slow:

    • deleting a lot of records
    • many indexes
    • missing indexes on foreign keys in child tables. (thank you to @CesarAlvaradoDiaz for mentioning this in the comments)
    • deadlocks and blocking
    • triggers
    • cascade delete (those ten parent records you are deleting could mean millions of child records getting deleted)
    • Transaction log needing to grow
    • Many Foreign keys to check

    So your choices are to find out what is blocking and fix it or run the deletes in off hours when they won't be interfering with the normal production load. You can run the delete in batches (useful if you have triggers, cascade delete, or a large number of records). You can drop and recreate the indexes (best if you can do that in off hours too).

    0 讨论(0)
  • 2020-12-23 00:05
    1. Disable CONSTRAINT

      ALTER TABLE [TableName] NOCHECK CONSTRAINT ALL;

    2. Disable Index

      ALTER INDEX ALL ON [TableName] DISABLE;

    3. Rebuild Index

      ALTER INDEX ALL ON [TableName] REBUILD;

    4. Enable CONSTRAINT

      ALTER TABLE [TableName] CHECK CONSTRAINT ALL;

    5. Delete again

    0 讨论(0)
  • 2020-12-23 00:05

    It's possible that other tables have FK constraint to your [table]. So the DB needs to check these tables to maintain the referential integrity. Even if you have all needed indexes corresponding these FKs, check their amount.

    I had the situation when NHibernate incorrectly created duplicated FKs on the same columns, but with different names (which is allowed by SQL Server). It has drastically slowed down running of the DELETE statement.

    0 讨论(0)
  • 2020-12-23 00:12

    Older topic but one still relevant. Another issue occurs when an index has become fragmented to the extent of becoming more of a problem than a help. In such a case, the answer would be to rebuild or drop and recreate the index and issuing the delete statement again.

    0 讨论(0)
提交回复
热议问题