I have 2 mysql servers. 1 node is master and the other acting as slave, replicating from the master
The 2 nodes have identical data and schema.
However, 1 particular query is executed differently from mysql when run on both nodes
query
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
Below is the outpout from EXPLAIN query run on both nodes. Note that the master node is outputting
the correct one
output on master node
+----+--------------------+-------+-----------------+-----------------------------+---------------------+---------+----------------+--------+----------------------------------------------+
| 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 |
+----+--------------------+-------+-----------------+-----------------------------+---------------------+---------+----------------+--------+----------------------------------------------+
output on slave node
+—-+——————–+——-+—————–+—————————–+——————+———+——————–+——–+—————————————-
------+
| 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 |
+----+--------------------+-------+-----------------+-----------------------------+------------------+---------+--------------------+--------+----------------------------------------------+
I cannot understand why this discrepancy exists. Any help?
Thanks
They can have different statistics for indexes / keys and that causes differences in index usage. If possible (locks table, so not always recommended) run ANALYZE TABLE for all participating tables and then query plan is likely same.