How to quickly conditionally delete massive amounts of data in SQL Server _mssql

Source: Internet
Author: User
Tags rollback
1.SQLSERVER database to change the bit type to not Boolean (that is, to change true to False;false to true)
Example: Update table set bit field =bit field-1
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 a certain statement to be not recorded in the log, if the execution of Delete Table1 where time < ' 2006-3-10 ' due to more records involved, so the log record is also very large (3-4g), if feasible, I recommend the following ways:

Select the records you want to keep to the new table. If you use full Recovery Mode
Depending on the number of records in the select INTO, the log may be larger
Select * into Table2 from Table1 Where time > = ' 2006-03-10 '

Then direct truncate TABLE1. No recovery mode is logged
Truncate Table Table1

Finally, the Table2 was renamed to Table1
EC sp_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.