今天注意到一个SQL查询,这是非常长的,我的mysql-的slow.log如何优化或正确写入这个MYSQL查询?
我想请教一些专家SQL如何正确格式化和执行该SQL。
sql背后的想法: 从2个表中返回所有不在mailchimp表中的电子邮件,并且仅返回DISTINCT值(用户和订阅者电子邮件可能会重复)。还包括城市和语言结果。
正如你所看到的query_time是怪物长,检查行只是跆拳道组合2表应该只有大约20K行。
Query_time: 113.216544 Lock_time: 0.000180 Rows_sent: 43 Rows_examined: 208280841
SELECT * FROM
(SELECT u.email AS email, u.city, u.language FROM users AS u
LEFT JOIN mailchimp AS m ON u.email = m.email WHERE m.email IS NULL GROUP BY u.email
UNION SELECT s.email AS email, s.city, s.language FROM subscribers AS s
LEFT JOIN mailchimp AS m ON s.email = m.email WHERE m.email IS NULL GROUP BY s.email)
AS sync GROUP BY sync.email ORDER BY sync.email ASC;
解释查询
+----+--------------+------------+------+---------------+------+---------+------+-------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+------+---------------+------+---------+------+-------+---------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 23 | Using temporary; Using filesort |
| 2 | DERIVED | u | ALL | NULL | NULL | NULL | NULL | 10482 | Using temporary; Using filesort |
| 2 | DERIVED | m | ALL | NULL | NULL | NULL | NULL | 11411 | Using where; Not exists |
| 3 | UNION | s | ALL | NULL | NULL | NULL | NULL | 2709 | Using temporary; Using filesort |
| 3 | UNION | m | ALL | NULL | NULL | NULL | NULL | 11411 | Using where; Not exists |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+------------+------+---------------+------+---------+------+-------+---------------------------------+
6 rows in set (2 min 1.65 sec)
子选择,工会,和顺序,哦,我的! – 2011-06-08 15:00:06
你可以将这些作为两个不同的查询并在代码中进行排序吗?可能会更快 – Ascherer 2011-06-08 15:00:49
对于该查询发布EXPLAIN也是如此。 – jishi 2011-06-08 15:10:02