这篇文章主要为大家详细介绍了SQL Server Execpt和not in 性能区别,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
主要讲 except 和 not in 的性能上的区别。代码如下:
CREATE TABLE tb1(ID int)
CREATE TABLE tb2(ID int)
BEGIN TRAN
DECLARE @i INT = 500
WHILE @i > 0
begin
INSERT INTO dbo.tb1
VALUES ( @i -- v - int
)
SET @i = @i -1
end
COMMIT我测试的时候tb1 是1000,tb2 是500
代码如下:
DBCC FREESYSTEMCACHE ('ALL','default');
SET STATISTICS IO ON
SET STATISTICS TIME on
SELECT * FROM tb1 EXCEPT SELECT * FROM tb2;
SELECT * FROM tb1 WHERE id NOT IN(SELECT id FROM tb2);--得不到任何值
SET STATISTICS IO OFF
SET STATISTICS TIME OFF
代码如下:
SELECT * FROM tb1 EXCEPT SELECT * FROM tb2;
|--Merge Join(Right Anti Semi Join, MERGE:([master1].[dbo].[tb2].[ID])=([master1].[dbo].[tb1].[ID]), RESIDUAL:([master1].[dbo].[tb1].[ID] = [master1].[dbo].[tb2].[ID]))
|--Sort(DISTINCT ORDER BY:([master1].[dbo].[tb2].[ID] ASC))
| |--Table Scan(OBJECT:([master1].[dbo].[tb2]))
|--Sort(DISTINCT ORDER BY:([master1].[dbo].[tb1].[ID] ASC))
|--Table Scan(OBJECT:([master1].[dbo].[tb1]))
代码如下:
SELECT * FROM tb1 WHERE id NOT IN(SELECT id FROM tb2);--得不到任何值
|--Hash Match(Right Anti Semi Join, HASH:([master1].[dbo].[tb2].[ID])=([master1].[dbo].[tb1].[ID]), RESIDUAL:([master1].[dbo].[tb1].[ID]=[master1].[dbo].[tb2].[ID]))
|--Table Scan(OBJECT:([master1].[dbo].[tb2]))
|--Nested Loops(Left Anti Semi Join)
|--Nested Loops(Left Anti Semi Join, WHERE:([master1].[dbo].[tb1].[ID] IS NULL))
| |--Table Scan(OBJECT:([master1].[dbo].[tb1]))
| |--Top(TOP EXPRESSION:((1)))
| |--Table Scan(OBJECT:([master1].[dbo].[tb2]))
|--Row Count Spool
|--Table Scan(OBJECT:([master1].[dbo].[tb2]), WHERE:([master1].[dbo].[tb2].[ID] IS NULL))
本文来自:http://www.q1010.com/179/7963-0.html
注:关于SQL Server Execpt和not in 性能区别的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:SQL SERVER
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。