询问者
性能优化

问题
全部回复
-
declare @LifeExt1 table (id int identity,col int) declare @i int;set @i=0 while @i<10000 begin insert into @LifeExt1 select @i set @i=@i+1 end declare @LifeExt2 table (id int identity,col int) declare @j int;set @j=2000 while @j<8000 begin insert into @LifeExt2 select @j set @j=@j+1 end --第一种情况 select col from @LifeExt1 where col not in ( select col from @LifeExt2) --0 时0 分10 秒16 毫秒 --第二种情况 select col from @LifeExt1 except select col from @LifeExt2 --0 时0 分5 秒983 毫秒
我用1万数据测试,结果显示第二种效率好。
-
恩,那就是说在同一种要求下,两种语句的执行效率有很大的差别?
从结果中我看出一点问题,not in 是经过服务器中间处理后按照排序读出结果
这是NOT in 的结果
COL
------------
0
1
2
3
4
5
6
7
8
9
10
11
12省略
而Except应该是没有经过中间处理后,算出结果的,得出来的数据是无序
这是 Except 结果
COL
------------
0
1589
23
1566
8513
46
215
1374
1543
69
192
1397省略
星光总能为我指引方向
- 已编辑 us_yunleiwang 2011年5月5日 9:23 内容不完整
-
看一看哪一个效率会更高呢?
select agentid from agent where agentid not in(select bank_node_id from LifeExt.dbo.BIS_M_BANK_NODE )
(select agentid from agent) except (select bank_node_id from LifeExt.dbo.BIS_M_BANK_NODE)
星光总能为我指引方向