Simple analysis in SQL Server about exists and in _mssql

Source: Internet
Author: User
Tags getdate

In and exists these two functions are similar, but because the optimization scheme is different, the not exists is usually faster than not, because the not exists can use the binding algorithm two not in, and the exists is not as fast as in, Because at this time in may use the binding algorithm more.

As shown, there are now two datasets, the left represents #temptable1, and the right side represents #temptable2. The following questions are now available:

1. Find the intersection of two sets?

2. Find the set of TempTable1 not belong to set #temptable2?

First create two temporary tables:

CREATE table #tempTable1 (argument1 nvarchar (), argument2 varchar (), Argument3 datetime, Argument4 int); Insert into #tempTable1 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher001 ', ' 13023218757 ', GETDATE () -1,1
);
Insert into #tempTable1 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher002 ', ' 23218757 ', GETDATE () -2,2);
Insert into #tempTable1 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher003 ', ' 13018757 ', GETDATE () -3,3);
Insert into #tempTable1 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher004 ', ' 13023257 ', GETDATE () -4,4);
Insert into #tempTable1 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher005 ', ' 13023218 ', GETDATE () -5,5);
Insert into #tempTable1 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher006 ', ' 13023218 ', GETDATE () -6,6);
Insert into #tempTable1 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher007 ', ' 13023218 ', GETDATE () -7,7); Insert into #tempTable1 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher008 ',' 13023218 ', GETDATE () -8,8);
CREATE table #tempTable2 (argument1 nvarchar (), argument2 varchar (), Argument3 datetime, Argument4 int); Insert into #tempTable2 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher001 ', ' 13023218757 ', GETDATE () -1,1
); Insert into #tempTable2 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher0010 ', ' 23218757 ', GETDATE () -10,10
);
Insert into #tempTable2 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher003 ', ' 13018757 ', GETDATE () -3,3);
Insert into #tempTable2 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher004 ', ' 13023257 ', GETDATE () -4,4);

 Insert into #tempTable2 (ARGUMENT1,ARGUMENT2,ARGUMENT3,ARGUMENT4) VALUES (' preacher009 ', ' 13023218 ', GETDATE () -9,9);

For example, I now refer to the argument1 of #temptable1 and #temptable2.

1. Find the intersection of two sets:

1) in mode

SELECT * from #tempTable2 where argument1 in
(select Argument1 from #tempTable1)

2) Exists way

SELECT * from #tempTable2 T2 where exists 
(SELECT * from #tempTable1 t1 where t1.argument1=t2.argument1)

2. Find the set of TempTable1 not belong to set #temptable2

1) in mode

SELECT * from #tempTable1 where argument1 isn't in
(select Argument1 from #tempTable2)

2) Exists way

SELECT * from #tempTable1 t1 where not exists 
(SELECT * from #tempTable2 T2 where t1.argument1=t2.argument1)

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.