How to quickly and conditionally Delete massive data in SQL SERVER

Source: Internet
Author: User

Recently, a friend asked me that it was very slow for him to delete millions or tens of millions of data from SQLSERVER. He analyzed the problem and gave some comments as follows, it may be useful to many people.

If your hard disk space is small and you do not want to set the database log to the minimum value (because you want other normal logs to Be RECORD), and the speed requirement is high, we recommend that you use turncate table1 to clear all the data, because truncate is a DDL operation and does not generate rollback, and does not write logs faster. If there is auto-increment, it will be restored to 1, delete generates rollback. If you delete a table with a large amount of data, it will be slow, consume a lot of rollback segments, and record G-level logs; of course, if conditional deletion such as where time <'1970-3-10 'is required, you can use delete to record logs, but the answer is not acceptable, the SQL Server engine is designed to log the Delete operation. So far, there is no way to force some statements not recorded in the log. If the execution of Delete Table1 where Time <'2017-3-10 'involves many records, therefore, the log record is large (3-4 GB). If it is feasible, we recommend that you use the following method:

Select the record you want to keep to the new table. If you use Full Recovery Mode
The log may be large based on the number of select into records
Select * into Table2 From Table1 Where Time> = '2017-03-10'

Then directly Truncate Table1. No log is recorded in whatever recovery mode: Truncate table Table1

Finally, rename Table2 to Table1: EC sp_rename 'table2' and '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.