SELECT a.*, u.avatar, u.name, u.surname, u.username, COUNT(a.user1) AS cnt
FROM user_actions a,users u,following f
WHERE a.user1=u.user_id AND
a.user1=f.follower_id AND
f.user_id=159
GROUP BY a.user1, a.action, day(a.dt)
ORDER BY a.id DESC
LIMIT 7;
查詢花費4.4909秒如何優化我的mysql查詢?
指標的表user_actions:
Action Keyname Type Unique Packed Column Cardinality Collation
Edit PRIMARY BTREE Yes No id 516094 A
Edit user1 BTREE No No user1 15639 A
Edit user2 BTREE No No user2 36863 A
Edit action BTREE No No action 16 A
Edit dt BTREE No No dt 516094 A
Edit group_index BTREE No No user1 20643 A
EXPLAIN SELECT a . * , u.avatar, u.name, u.surname, u.username, COUNT(a.user1) AS cnt
FROM user_actions a, users u, following f
WHERE a.user1 = u.user_id
AND a.user1 = f.follower_id
AND f.user_id =159
GROUP BY a.user1, a.action, day(a.dt)
ORDER BY a.id DESC
LIMIT 7
ID SELECT_TYPE表型possible_keys鍵key_len REF行 額外1 SIMPLE F ref時USER_ID,follower_id,for_actions for_actions 4 const 242使用索引;使用臨時;使用文件排序1 SIMPLE 一個裁判USER1,GROUP_INDEX GROUP_INDEX 4 pictify_main.f.follower_id 25 1簡單U eq_ref PRIMARY PRIMARY 4 pictify_main.a.user1 1使用其中
您可以嘗試在查詢之前添加EXPLAIN以獲取其詳細分析。比你可以在這裏發佈結果,所以我們將能夠看到什麼是錯的。 – magnetik