Delete statement in SQL is very slow

前端 未结 15 689
自闭症患者
自闭症患者 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:13

    After inspecting an SSIS Package(due to a SQL Server executing commands really slow), that was set up in a client of ours about 5-4 years before the time of me writing this, I found out that there were the below tasks: 1) insert data from an XML file into a table called [Importbarcdes].

    2) merge command on an another target table, using as source the above mentioned table.

    3) "delete from [Importbarcodes]", to clear the table of the row that was inserted after the XML file was read by the task of the SSIS Package.

    After a quick inspection all statements(SELECT, UPDATE, DELETE etc.) on the table ImportBarcodes that had only 1 row, took about 2 minutes to execute.

    Extended Events showed a whole lot PAGEIOLATCH_EX wait notifications.

    No indexes were present of the table and no triggers were registered.

    Upon close inspection of the properties of the table, in the Storage Tab and under general section, the Data Space field showed more than 6 GIGABYTES of space allocated in pages.

    What happened:

    The query ran for a good portion of time each day for the last 4 years, inserting and deleting data in the table, leaving unused pagefiles behind with out freeing them up.

    So, that was the main reason of the wait events that were captured by the Extended Events Session and the slowly executed commands upon the table.

    Running ALTER TABLE ImportBarcodes REBUILD fixed the issue freeing up all the unused space. TRUNCATE TABLE ImportBarcodes did a similar thing, with the only difference of deleting all pagefiles and data.

提交回复
热议问题