sql-not in 与not exists效率
sql-------not in 与not exists效率
总结: not exists总比not in好, in在内表大时好,exists在内表小时好 大表,数据量2229073 小表,数据量77989 注:两个表都没有索引 试验结果: 轮次 内外表 测试对象 用时(秒) 结果数据量 --1.1 外大内小 not in 1:29 2176395 --2.1 外大内小 not exists 0:30 2176395 --1.2 外大内小 in 0:03 52681 --2.2 外大内小 exists 0:02 52681 --1.3 外小内大 in 0:02 52681 --2.3 外小内大 exists 0:05 52681 --1.4 外小内大 not in 0:05 25311 --2.4 外小内大 not exists 0:04 25311 结果分析: 不受表大小影响,not exists一直表现不错,在外大内小的时候表现尤佳;外表大内表小的时候,exists占微弱优势;外表小内表大的时候,in占优势;not in,貌似不行啊。 注:结果会受数据量和索引等因素的影响,所以实际应用中建议多做测试,以达到最优化效果。 顺便说一下,in在罗列有限集合的时候还是不错的,跟用or和=相差不大,但起码方便了,特别是数量多的时候;not in在罗列有限集合的时候,我用7262656条记录的表做测试,还是用and和<>连接会快一些。
1 楼
oo-java
2011-08-08
select t1.s_id from sc t1 where t1.c_id not in (select t2.c_id from course t2 where t2.c_id > 5);
select t1.s_id from sc t1 where not exists (select null from sc t2 where t2.c_id > 5 and t2.c_id = t1.c_id);
not in 比 not exists 效率好100倍以上,所以不要说的这么绝对
select t1.s_id from sc t1 where not exists (select null from sc t2 where t2.c_id > 5 and t2.c_id = t1.c_id);
not in 比 not exists 效率好100倍以上,所以不要说的这么绝对