Compare] [SQL Server performance optimization method to delete large amounts of data

: Original relatively] [SQL Server performance optimization method to delete large amounts of data


If you want to remove large amounts of data tables, this generally refers to a large number of deleted records more than 10%, then how to delete, efficiency will be relatively high? The deletion will not affect how the system is relatively small it?


Below do a test, then the results of this experiment are analyzed, and then draw conclusions.


1. Create a database


   
   
  1. use master
  2. go
  3. if exists( select * from sys.databases where name = 'test')
  4. drop database test
  5. go
  6. create database test
  7. go

2, create a table


   
   
  1. use test
  2. go
  3. if exists( select * from sys.tables where name = 't')
  4. drop table t
  5. go
  6. create table t(i int,v varchar( 100) default replicate( 'a', 100)
  7. ,vv varchar( 100) default replicate( 'a', 100),
  8. vvv varchar( 100) default replicate( 'a', 100));

3, insert data

Add 100,000 records with the following code, 9 seconds consumption:


   
   
  1. declare @i int;
  2. set @i = 1
  3. begin tran
  4. while @i <= 100000
  5. begin
  6. insert into t(i) values(@i)
  7. set @i = @i + 1
  8. end
  9. commit tran

If using the following code, add the records 100,000, consumed 43 seconds:


   
   
  1. declare @i int;
  2. set @i = 1
  3. while @i <= 100000
  4. begin
  5. begin tran
  6. insert into t(i) values(@i) --没执行一次就提交一次,效率较差
  7. commit tran
  8. set @i = @i + 1
  9. end

Double insertion data consumes 1 minutes 38 seconds


   
   
  1. insert into t
  2. select *
  3. from t
  4. go 6

Finally, a total of 6.4 million inserted data.


4, indexing

create index idx_t_idx1 on t(i)

  
  

5, the following settings, in order to prevent SQL Server uses too much memory, which led to crash


   
   
  1. sp_configure ' show advanced option ',1
  2. go
  3. reconfigure
  4. go
  5. sp_configure ' max server memory (MB) ',3584
  6. go
  7. reconfigure
  8. go

6, the data created above the table t, t1 and t2 copy into two tables, index the table t1


   
   
  1. if exists( select * from sys.tables where name = 't1')
  2. drop table t1
  3. go
  4. select * into t1
  5. from t
  6. create index idx_t1_idx1 on t1(i)
  7. go
  8. if exists( select * from sys.tables where name = 't2')
  9. drop table t2
  10. go
  11. select * into t2
  12. from t

7, t1 table delete operation to delete the number 1000, each with a number 64, so that each deletion 64,000. 1000 total deletion, so delete 640,000 records, total time 82 seconds


   
   
  1. dbcc dropcleanbuffers
  2. go
  3. declare @i int = 20000;
  4. declare @start_time datetime; -- = getdate();
  5. while @i <30000
  6. begin
  7. set @start_time = GETDATE();
  8. delete from t1 where I>=@i and i<=@i + 999
  9. set @i += 1000
  10. select DATEDIFF( second,@start_time, getdate())
  11. end

8, deleting the data table t2, it takes 44 seconds


   
   
  1. delete from t2
  2. where I>= 20000 and i< 30000


Through the above tests found that:

 

1, when a large number of insert operations, the operation after the completion of submission, submitted immediately after each insertion ratio, higher efficiency.

 

2, when you delete large amounts of data, even if the use of the index, even while the use of the index and batch operation, efficiency is not no such index, delete directly to the high table scan.  


But the problem is that table scans will lock the entire table, block other transactions, leading to paralysis of large-scale systems business.

So, while fast through a direct method will delete speed, but if the index and by batch, you will need to remove the lock of a batch of data, and other data will not be locked, blocking problems caused by much smaller .

 

3, so the combination of the above two points, when the high-volume operation, if the final submission, the higher the efficiency of the entire operation, but can cause problems blocking, because it is not submitted in time will lead to other transactions are blocked.


Similarly, it may be deleted directly through higher efficiency, but will lock table, can cause serious blocking problems, and through the index and batch process, although the efficiency is not too high, but you can batch process, the equivalent of the batch submitted, and each lot by the index, only lock the records need to be addressed, while other records are not locked, then it is unlikely to cause problems blocking.


So, a huge delete operation, if by a full table scan, carried out in the evening for system maintenance more free time; but if you have to perform during the day, consider batching through the index and to reduce the problem of congestion, but the system still will have some impact, particularly in terms of memory.


Published 416 original articles · won praise 135 · views 940 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12019953.html