說明: Complaint_status包含特定的投訴多行,試圖讓用戶明智的封閉投訴(狀態= 2和值IN(1,2))建議優化MySQL查詢
痛點:
YEAR(cs.created_at)=year(curdate())
AND MONTH(cs.created_at)=month(curdate())
以本地幾乎5分鐘,並在服務器
SELECT u3.name,COUNT(c3.id) as closed_tickets
FROM complaint c3,complaint_status cs,assignment a3,user u3
WHERE
c3.id=cs.complaint_id
AND a3.complaint_id=c3.id
AND u3.id=a3.assigned_to
AND u3.user_type=14
AND a3.expiry IS NULL
AND cs.id IN(SELECT MAX(id) FROM complaint_status WHERE complaint_id=c3.id AND status=2 AND value IN(1,2))
AND YEAR(cs.created_at)=year(curdate())
AND MONTH(cs.created_at)=month(curdate())
GROUP BY u3.id
ORDER BY COUNT(c3.id) DESC
將這個查詢之前的解釋,並與結果編輯你的問題。 – Mihai 2014-10-28 09:26:50
你不明白看這裏http://www.sitepoint.com/using-explain-to-write-better-mysql-queries/ – Mihai 2014-10-28 09:31:47