SQL Server quickly conditionally deletes massive data

Source: Internet
Author: User
Tags rollback

Recently a friend asked me, he said that he deleted millions of to tens of millions of data in SQL Server is very slow, to help him analyze, some of the following comments, perhaps useful for many people, and also good long did not write a blog, together to explore

If your hard disk space is small and you do not want to set the database log to be minimal (because you want other normal logs to still be logged), and high speed requirements, and clear all the data suggest you use Turncate table1, because truncate is a DDL operation, Do not produce rollback, do not write log faster, and then if there is an increase, restore to the beginning of 1, and delete will produce rollback, if you delete large data table speed will be very slow, and will occupy a lot of rollback segments, At the same time to record the G-level log, of course, if there are conditions to delete such as where time< ' 2006-3-10 ' How to do, can not log with delete, the answer is not, the SQL Server engine design on the delete operation will be logged. So far there is no way to force some statements are not recorded in the log, if the implementation of the deletetable1wheretime< ' 2006-3-10 ' due to the number of records involved, so the log record is also very large (3-4g), if feasible, I suggest the following ways:

Select the records you want to keep to the new table. If you use Fullrecoverymode

Depending on the number of records in the Selectinto, the log may be larger

select*intotable2fromtable1wheretime>= ' 2006-03-10 '

Then direct TruncateTable1. No recovery mode is logged

TruncatetableTable1

Finally, the Table2 was renamed to Table1

Ecsp_rename ' Table2 ', ' Table1 '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.