查询优化器的奇怪的mysql问题

时间:2012-07-17 06:35:05

标签: mysql optimization

我有2个mysql服务器。 1个节点是主节点,另一个节点作为从节点,从主节点复制

2个节点具有相同的数据和架构。 但是,在两个节点上运行时,1个特定查询的执行方式与mysql不同

查询

EXPLAIN SELECT t.*, COUNT(h.id)
                         FROM tags t
                         INNER JOIN tags2articles s
                         ON t.id = s.tag_id
                         INNER JOIN tag_hits h
                         ON h.id = s.tag_id
                         INNER JOIN articles art
                         ON art.id = s.`article_id`
                         WHERE art.source_id IN (SELECT id FROM feeds WHERE source_id = 15074)
                         AND time_added > DATE_SUB(NOW(), INTERVAL 1 DAY)
                         AND t.type = '1'
                         GROUP BY t.id
                         HAVING COUNT(h.id) > 4
                         ORDER BY COUNT(h.id) DESC
                         LIMIT 15

以下是在两个节点上运行EXPLAIN查询的输出。请注意,主节点正在输出 正确的

主节点输出

+----+--------------------+-------+-----------------+-----------------------------+---------------------+---------+----------------+--------+----------------------------------------------+
| id | select_type        | table | type            | possible_keys               | key                 | key_len | ref            | rows   | Extra                                        |
+----+--------------------+-------+-----------------+-----------------------------+---------------------+---------+----------------+--------+----------------------------------------------+
|  1 | PRIMARY            | art   | ALL             | PRIMARY                     | NULL                | NULL    | NULL           | 100270 | Using where; Using temporary; Using filesort |
|  1 | PRIMARY            | s     | ref             | PRIMARY,FK_tags2articles    | FK_tags2articles    | 4       | art.id         |     12 | Using index                                  |
|  1 | PRIMARY            | h     | ref             | tags_hits_idx               | tags_hits_idx       | 4       | s.tag_id       |      1 | Using index                                  |
|  1 | PRIMARY            | t     | eq_ref          | PRIMARY,tags_type_idx       | PRIMARY             | 4       | s.tag_id       |      1 | Using where                                  |
|  2 | DEPENDENT SUBQUERY | feeds | unique_subquery | PRIMARY,f_source_id_idx     | PRIMARY             | 4       | func           |      1 | Using where                                  |
+----+--------------------+-------+-----------------+-----------------------------+---------------------+---------+----------------+--------+----------------------------------------------+
从属节点上的

输出

+ ---- + -------------------- + ------- + ------------ ----- + ----------------------------- + -------------- ---- + --------- + -------------------- + -------- + ----- -----------------------------------

------+
| id | select_type        | table | type            | possible_keys               | key              | key_len | ref                | rows   | Extra                                        |
+----+--------------------+-------+-----------------+-----------------------------+------------------+---------+--------------------+--------+----------------------------------------------+
|  1 | PRIMARY            | t     | ref             | PRIMARY,tags_type_idx       | tags_type_idx    |  2      | const              | 206432 | Using where; Using temporary; Using filesort |
|  1 | PRIMARY            | h     | ref             | tags_hits_idx               | tags_hits_idx    | 4       | t.id               |      1 | Using index                                  |
|  1 | PRIMARY            | s     | ref             | PRIMARY,FK_tags2articles    | PRIMARY          | 4       | h.id               |      2 | Using where; Using index                     |
|  1 | PRIMARY            | art   | eq_ref          | PRIMARY                     | PRIMARY          | 4       | s.article_id       |      1 | Using where                                  |
|  2 | DEPENDENT SUBQUERY | feeds | unique_subquery | PRIMARY,f_source_id_idx     | PRIMARY          | 4       | func               |      1 | Using where                                  |
+----+--------------------+-------+-----------------+-----------------------------+------------------+---------+--------------------+--------+----------------------------------------------+

我无法理解为什么存在这种差异。有什么帮助吗?

由于

1 个答案:

答案 0 :(得分:1)

它们可以具有索引/键的不同统计信息,并且会导致索引使用情况的差异。如果可能(锁定表,因此并不总是建议)对所有参与表运行ANALYZE TABLE,然后查询计划可能相同。