分享

SQL查询速度测试及Not Exists

 nhcyy 2006-07-28
SQL查询速度测试及Not Exists
2006-07-10 22:46:25
  
功能:从A表中选出B表中没有的记录
在SQL Server2000作如下测试:
/*建立两个表A、B */
use lzw_test
create table a(
id int identity primary key
)
create table b(
id int identity primary key
)


/*A表中插入50万条记录,B表中插入20万条记录*/
set identity_insert a on
set @i=0
while @i<$500000
begin
set @i=@i+1
insert into a (id) values (@i)
end
执行时间4分钟多
set @i=0
while @i<$200000
begin
set @i=@i+1
insert into b (id) values (@i)
end
执行时间2分钟多
set identity_insert b off
select a.id from a where not exists(select * from b where a.id=b.id)
此条查询语句用了0.1秒,汗~
上条语句不能写成:
select a.id from a where a.id not exists(select b.id from b)
同时exists中的where 后的条件所用的字段也就是要进行exists匹配的字段,而并不是进行匹配或过滤功能。
exists和in的区别:
exists是用于记录集的,即一行一行。
in是用于单个字符或数字,如:in(‘a‘,‘b‘)
另外mysql数据库不支持exists.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多