以下查詢運行速度慢(超過五秒鐘)以約五百萬條記錄在每個表:優化MySQL的加入帶濾波器和訂單上不同的表
SELECT DISTINCT `items`.*
FROM `items`
INNER JOIN `tags` ON `tags`.`item_id` = `items`.`id`
WHERE `tags`.`name` = '...'
ORDER BY `items`.`stars` DESC LIMIT 64;
我不清楚的最佳策略指數。我最初的想法是增加一個複合索引items.id
+ items.stars
和複合索引tags.item_id
+ tags.name
- 但這並沒有顯着減少查詢時間。我在所有外鍵索引和items.stars
和tags.name
索引。
EXPLAIN
顯示我的兩個指標(index_tags_on_item_id_and_name
和index_items_on_id_and_stars
)作爲可能的密鑰,但沒有使用:
1 | SIMPLE | tags | ref | index_tags_on_name | 5 | const | 326538 | Using where; Using temporary; Using filesort
2 | SIMPLE | items | eq_ref | PRIMARY | 4 | tags.item_id | 1 |
如何繼續進行,一面和秩序的標準在加入任何想法或最佳實踐另一個?我現在唯一的想法是複製tags
中的stars
。
你確定你需要'DISTINCT'? –