以下查询运行速度慢(超过五秒钟)以约五百万条记录在每个表:优化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'? –