A sql tuning experience

/*
    TRUNCATE TABLE dbo.temp_new_skc;
    insert into dbo.temp_new_skc
	    select A.SkuCode,a.Quantity, a.C_date from OmsSale a  WHERE CONVERT(varchar(100), C_date, 23) >= dateadd(YY,1,@B_time) AND CONVERT(varchar(100), C_date, 23)<= dateadd(YY,1,@E_time)
	and not exists (select 1 from OmsSale b  WHERE CONVERT(varchar(100), C_date, 23) >= @B_time AND CONVERT(varchar(100), C_date, 23)<= @E_time  and a.SkuCode = b.SkuCode)
	  and a.StoreName not like '%Vipshop%'; -- predict to exclude the wj 9-19 of Vipshop
    */
    if(object_id('temp_new_skc','U')) is not null
		drop table temp_new_skc;
      select A.SkuCode,a.Quantity, a.C_date
	   into temp_new_skc
	  from OmsSale a  WHERE CONVERT(varchar(100), C_date, 23) >= dateadd(YY,1,@B_time) AND CONVERT(varchar(100), C_date, 23)<= dateadd(YY,1,@E_time)
	and not exists (select 1 from OmsSale b  WHERE CONVERT(varchar(100), C_date, 23) >= @B_time AND CONVERT(varchar(100), C_date, 23)<= @E_time  and a.SkuCode = b.SkuCode)
	  and a.StoreName not like '%Vipshop%' ;

 1. Select ino (requires that the table does not exist) is much faster than insert into select (requires that the table exists). See the time comparison below

2. truncate is much faster than delete

3. The composite index column order must be consistent with the where column order

4. Even if the index is built, the efficiency is not necessarily fast

5. The consumption of the query should not be as large as that of the insert, and the insert of a million level takes nearly 1 minute.



 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326070340&siteId=291194637