MySQL 行子查詢最佳化

來源:互聯網
上載者:User

標籤:mysql 行子查詢最佳化

1.什麼是行子查詢:

select t1.*,(select vn from t2 where t2.c.1=t1.c3 limit 1) where t1.cn=‘xxx‘ .... 

類似每行通過一個子查詢來查詢獲的結果,都是行子查詢。

2.案例:

MySQL 版本:MySQL-5.6.16-log

原sql:

# Query_time: 20.769287  Lock_time: 0.000152 Rows_sent: 10  Rows_examined: 11665408SET timestamp=1420020764;SELECT f3.id,f3.pin,f3.toUser,f3.content,f3.created_time,f3.modified_time,f3.imgUrl,f3.clientVersion,f3.deviceInfo,case WHEN f3.created_time>f3.reply_time or f3.reply_time is NULL THEN ‘0‘ ELSE ‘1‘ END as reply_statusfrom (select f1.id,f1.pin,f1.toUser,f1.content,f1.created_time,f1.modified_time,f1.imgUrl,f1.clientVersion,f1.deviceInfo,(select f2.created_time from feedback_xx f2 where pin=‘SYSTEM‘ and f2.toUser=f1.pin order by f2.created_time desc limit 0,1) as  reply_timeFROM feedback_xx f1 GROUP BY f1.pin DESC ORDER BY f1.created_time DESC)f3 where 1=1 limit 0,10;

查詢20多秒出結果,原因是外表的每一行都要通過子查詢獲得結果。存在行子查詢

select f2.created_time from feedback_info f2 where pin=‘SYSTEM‘ and f2.toUser=f1.pin order by f2.created_time desc limit 0,1;


通過left join方式改寫sql,目的減少內表的掃描次數。

最佳化成:

SELECT f3.id,f3.pin,f3.toUser,f3.content,f3.created_time,f3.modified_time,f3.imgUrl,f3.clientVersion,f3.deviceInfo,case WHEN f3.created_time>f3.reply_time or f3.reply_time is NULL THEN ‘0‘ ELSE ‘1‘ END as reply_status from(select  f1.id,f1.pin,f1.toUser,f1.content,f1.created_time,f1.modified_time,f1.imgUrl,f1.clientVersion,f1.deviceInfo,f2.created_time as reply_time from feedback_xx f1 left join(select a.toUser,a.created_time from feedback_xx a where a.pin=‘SYSTEM‘ order by a.created_time desc)f2on f1.pin=f2.toUserGROUP BY f1.pin DESC ORDER BY f1.created_time DESC)f3 where 1=1;

5274 rows in set (0.17 sec)

執行計畫:

+----+-------------+------------+------+---------------+-------------+---------+----------------+-------+----------------------------------------------------+| id | select_type | table      | type | possible_keys | key         | key_len | ref            | rows  | Extra                                              |+----+-------------+------------+------+---------------+-------------+---------+----------------+-------+----------------------------------------------------+|  1 | PRIMARY     | <derived2> | ALL  | NULL          | NULL        | NULL    | NULL           | 98773 | NULL                                               ||  2 | DERIVED     | f1         | ALL  | idx_pin       | NULL        | NULL    | NULL           |  9846 | Using temporary; Using filesort                    ||  2 | DERIVED     | <derived3> | ref  | <auto_key0>   | <auto_key0> | 194     | jrlicai.f1.pin |    10 | NULL                                               ||  3 | DERIVED     | a          | ref  | idx_pin       | idx_pin     | 194     | const          |  2207 | Using index condition; Using where; Using filesort |+----+-------------+------------+------+---------------+-------------+---------+----------------+-------+----------------------------------------------------+

總結:在統計查詢中不要使用行子查詢,效率很低,一定要改寫成join 的方式。


附:2014 年最後一篇Blog,明年繼續...... 

本文出自 “My DBA life” 部落格,請務必保留此出處http://huanghualiang.blog.51cto.com/6782683/1598203

MySQL 行子查詢最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.