我有一個簡單的查詢,並按順序(與連接),但它似乎沒有工作沒有臨時表和文件。下面是該查詢:正確的MySQL索引這個簡單的查詢
SELECT items.name, max(activity.created_at), count(items.item_id) as total_count
FROM activity
INNER JOIN items ON activity.item_id = items.item_id
WHERE activity.user_id = 'XXX'
GROUP BY activity.item_id
order by activity.created_at desc
LIMIT 0, 15
我有一個指標:
(activity.item_id, activity.user_id, activity.created_at) - idx_item_created
(activity.user_id, activity.created_at) - user_id_2
當我強迫idx_item_created它進行全表掃描,默認情況下它運行使用user_id_2和總產:
Using where; Using temporary; Using filesort
無論如何優化此查詢更好?
我建議給max(activity.created_at)一個別名並按此排序。 – 2015-02-08 14:48:13