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.
|
|