SQL Server optimizations alternatives to in and not

Source: Internet
Author: User

Not in


SET STATISTICS time on
Go
--Backup data
Insert into Bakinfo (id,pname,remark,impdate,ups tutorial Tate)
Select Id,pname,remark,impdate,upstate from dbo. Info
where ID not in (SELECT ID from Dbo.bakinfo)
Go
SET STATISTICS Time off


SQL Server profiling and compilation time:
CPU time = 0 milliseconds, elapsed time = 3 milliseconds.
SQL Server Execution Time:
CPU time = 453 milliseconds, elapsed time = 43045 milliseconds.
(100000 rows affected)
SQL Server profiling and compilation time:
CPU time = 0 milliseconds, elapsed time = 1 milliseconds.
--Change the current table state
Update Info set upstate=1 where ID in (select ID from Dbo.bakinfo)


CREATE PROCEDURE AddData
As
DECLARE @id int
Set @id =0
while (@id <100000)
Begin
INSERT INTO dbo. Info (Id,pname,remark,impdate,upstate)
VALUES (@id, convert (varchar, @id) + ' 0 ', ' abc ', GETDATE (), 0)
Set @id = @id +1
End
EXEC AddData


SQL Server profiling and compilation time:
CPU time = 62 milliseconds, elapsed time = 79 milliseconds.
SQL Server Execution Time:
CPU time = 188 milliseconds, elapsed time = 318 milliseconds.
(100000 rows affected)
SQL Server profiling and compilation time:
CPU time = 0 milliseconds, elapsed time = 1 milliseconds.
--Delete Current table data
Delete from Info where upstate=1 and IDs in (select IDs from Dbo.bakinfo)


SET STATISTICS time on
Go
--Backup data
Insert into Bakinfo (id,pname,remark,impdate,upstate)
Select Id,pname,remark,impdate,upstate from
(SELECT info.id,info.pname, Info.remark, Info.impdate,info.upstate, bakinfo.id as Bakid
From Info left JOIN
Bakinfo on info.id = bakinfo.id) as T
Where T.bakid is null and t.upstate=0
Go
SET STATISTICS time off;


SQL Server parse and compile time:
CPU time = 247 milliseconds, elapsed time = 247 milliseconds.
SQL Server Execution time:
CPU time = 406 milliseconds, elapsed time = 475 milliseconds.
(100000 rows affected)
SQL Server parse and compile time:
CPU time = 0 milliseconds, elapsed time = 1 milliseconds.
--Change the current table state
Update Info set upstate=1
from Info INNER JOIN
bakinfo on info.id = bakinfo.id

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.