MySQL子查询优化实例
2013-10-25 15:41
411 查看
[b]优化:子查询改写成关联查询[/b]
线上遇到问题,查询较慢,如为对应SQL的查询执行计划:
localhost.user>explain EXTENDED select uid from bar_user_6 where bid='9279' and uid in (SELECT a_uid FROM `user_atten_3` WHERE uid='1400696003') limit 12\G *************************** 1. row *************************** id: 1 select_type: PRIMARY table: bar_user_6 type: ref possible_keys: bid key: bid key_len: 4 ref: const rows: 581295 filtered: 100.00 Extra: Using where; Using index *************************** 2. row *************************** id: 2 select_type: DEPENDENT SUBQUERY table: user_atten_3 type: eq_ref possible_keys: uid key: uid key_len: 30 ref: const,func rows: 1 filtered: 100.00 Extra: Using where; Using index 2 rows in set, 1 warning (0.00 sec)
优化方案,改写成关联查询
localhost.user>explain extended select bar.uid from bar_user_6 as bar ,user_atten_3 as user where bid='9279' and bar.uid = user.a_uid and user.uid='1400696003' limit 12\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: user type: ref possible_keys: uid key: uid key_len: 22 ref: const rows: 1 filtered: 100.00 Extra: Using where; Using index *************************** 2. row *************************** id: 1 select_type: SIMPLE table: bar type: eq_ref possible_keys: bid key: bid key_len: 12 ref: const,user.user.a_uid rows: 1 filtered: 100.00 Extra: Using index
子查询的查询过程(循环嵌套):
for each row in t1 matching range { for each row in t2 matching reference key { for each row in t3 { if row satisfies join conditions, send to client } } }
参考文档:
http://dev.mysql.com/doc/refman/5.5/en/nested-loop-joins.html
http://hidba.org/?p=819
http://hidba.org/?p=624
http://hidba.org/?p=743
相关文章推荐
- MYSQL子查询和嵌套查询优化实例解析
- mysql 查询语句优化实例
- MySQL子查询的优化
- 实例详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(二)
- mysql通过“延迟关联”进行limit分页查询优化的一个实例
- 实例讲解MYSQL数据库的查询优化技术
- [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时(转)
- MySQL随机查询的优化实例
- Mysql子查询优化技术
- MySQL关联查询优化实例
- 实例详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(一)
- mysql数据库索引查询一个优化大数据量的实例的分享
- MySQL子查询优化---详解--1
- [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时
- 实例讲解MySQL数据库的查询优化技术
- MySQL慢查询优化之慢查询日志分析的实例教程
- 实例讲解MySQL数据库的查询优化技术
- MySQL子查询优化---详解--2
- [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时
- Django中的QuerySet查询优化之实例篇