上一篇 <<<MySQL性能优化之关联查询优化
下一篇 >>>order by中的单路和双路排序算法原理
优化原则
1.使用in场景
如果jarye_b中的数据小于jarye_A的数据可以使用in
select * from jarye_a where id in (select id from jarye_b)
底层类似于这样实现
for(select id from jarye_B){
select * from jarye_a where jarye_a.id = jarye_b.id
}
2.使用exists场景
如果jarye_a中的数据小于jarye_b数据可以使用exists
select * from jarye_a where exists (select id from jarye_b where jarye_b.id = jarye_a.id)
底层类似于这样实现
for(select * from jarye_a){
select * from jarye_a where jarye_b.id = jarye_a.id
}
3.用between替代in
select id from t where num in(1,2,3)
//对于连续的数值,能用 between 就不要用 in 了:
select id from t where num between 1 and 3
4.用in替换or(针对少数,多数则不见得好)
低效查询
SELECT * FROM t WHERE LOC_ID = 10 OR LOC_ID = 20 OR LOC_ID = 30;
----->
高效查询
SELECT * FROM t WHERE LOC_IN IN (10,20,30);
推荐阅读:
<<<MySQL执行计划示例解读
<<<MySQL性能优化之慢查询定位
<<<MySQL性能优化之表设计优化
<<<MySQL性能优化之常用SQL语句优化
<<<MySQL性能优化之索引调优实战
<<<MySQL性能优化之分页查询优化
<<<MySQL性能优化之关联查询优化
<<<order by中的单路和双路排序算法原理
<<<MySQL如何性能优化面试题完美解答