我有這樣的查詢來執行:MySQL查詢 - 幫助改進
SELECT u1.firstname,
u1.lastname,
u1.totalcommission,
u1.userid,
count(DISTINCT u2.userid) AS level1,
count(DISTINCT u3.userid) AS level2
FROM users AS u1
INNER JOIN users AS u2 ON u1.userid = u2.affiliate1
AND u2.signupdate BETWEEN '2011-01-01 00:00:00' AND '2013-04-02 23:59:59'
LEFT JOIN users AS u3 ON u1.userid = u3.affiliate2
WHERE u1.usertypeid != '1'
GROUP BY u1.userid
ORDER BY count(DISTINCT u2.userid) DESC
表的索引:
PRIMARY BTREE Yes No userid 41318 A No
email BTREE Yes No email 41318 A No
affiliate1 BTREE No No affiliate1 1967 A Yes
affiliate2 BTREE No No affiliate2 258 A Yes
signupdate BTREE No No signupdate 41318 A No
查詢工作,但問題是,實在是太慢了(在表用戶有43k行,沒有更多),大約需要10秒,由於計數(不同)的功能。有沒有辦法用更好的表現取代它?
感謝,
/盧卡
你發佈了所有表的結構嗎?桌子上的索引是什麼? –
您可以在查詢前添加「DESC」並將該查詢的輸出添加到您的問題中嗎?該查詢將顯示您在哪裏以及使用哪些密鑰。 –