你的位置:首页 > 软件开发 > 数据库 > mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录

mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录

发布时间:2015-07-14 15:00:17
mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录,需要的朋友可以参考下。 NOT IN、JOIN、IS NULL、NOT EXISTS效率对比 语句一:select count(*) from A where A.a not i ...

mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录,需要的朋友可以参考下。

 

NOT IN、JOIN、IS NULL、NOT EXISTS效率对比 语句二:select count(*) from A left join B on A.a = B.a where B.a is null 语句三:select count(*) from A where not exists (select a from B where A.a = B.a) 知道以上三条语句的实际效果是相同的已经很久了,但是一直没有深究其间的效率对比。一直感觉上语句二是最快的。 假设buffer pool足够大,写法二相对于写法一来说存在以下几点不足: 这三点综合起来,在处理海量数据时就会产生比较明显的区别(主要是内存和CPU上的开销)。我怀疑楼主在测试时buffer pool可能已经处于饱和状态,这样的话,写法二的那些额外开销不得不借助磁盘上的虚拟内存,在SQL Server做换页时,由于涉及到较慢的I/O操作因此这种差距会更加明显。 关于日志文件过大,这也是正常的,因为删除的记录多嘛。可以根据数据库的用途考虑将恢复模型设为simple,或者在删除结束后将日志truncate掉并把文件shrink下来。 mysql not in 和 left join 效率问题记录 ----------------------------+ Extra | ----------------------------+ Using where | Using index; Using where | Using where; Using temporary | ----------------------------+ -------+ | -------+ | | temporary | temporary | -------+ --------------------+ | --------------------+ Using temporary | Not exists; Distinct | --------------------+ ------------------------+ | ------------------------+ where; Using temporary | where | ------------------------+ 为了验证数据查询效率,将上述查询中的subjectID =12的限制条件去掉,结果统计查询时间如下 laserhe帮忙分析问题总结

复制代码 代码如下:----+ | ----+ | | temporary | ----+ 数据库优化的基本原则:让笛卡尔积发生在尽可能小的集合之间,mysql在join的时候可以直接通过索引来扫描,而嵌入到子查询里头,查询规 划器就不晓得用合适的索引了。 的索引,然后根据具体情况做一个排列组合,然后计算这个排列组合中的每一种的开销(类似explain的输出的计算机可读版本),然后比较里 面开销最小的,选取并执行之。那么: and CreateTime<='2009-8-17 16:00:00' ) a left join UserMsg b on a.ruid = b.ruid and b.createTime < '2009-8-14 15:30:00' where b.ruid is null; ------------------+ | ------------------+ | Using where | Using temporary | ------------------+ and CreateTime<='2009-8-17 16:00:00' ) a left join ( select distinct RUID from UserMsg where createTime < '2009-8-14 15:30:00' ) b on a.ruid = b.ruid where b.ruid is null; -+ | -+ | | | | -+

原标题:mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录

关键词:MYSQL

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。