我知道有一千个类似的问题,我读了很多,但我不知道如何提高以下查询的性能:
SELECT DATE_FORMAT(c.date, "%d/%m/%Y") AS ddmmyy, c.idQualification, q.name,
SUM(idContactList = "") + COUNT( DISTINCT NULLIF( CONCAT( c.idContactList, c.idActivity ), "" ) ) AS numCalls
FROM callsBreakdown c
LEFT JOIN qualification q ON q.idQualification = c.idQualification
LEFT JOIN activityCampaign a ON (a.idActivity = c.idActivity)
LEFT JOIN qualificationCampaign qc ON (qc.idCampaign = a.idCampaign AND qc.idQualification = c.idQualification)
WHERE c.date BETWEEN "2011-01-01" AND "2012-10-01"
GROUP BY c.date, q.name
如果日期范围很宽,例如 1 年或更长时间,则查询需要 10-12 秒才能完成。在关于 callBreakdown 表的 EXPLAIN 中(其他在 Extra 列中有“使用索引”并且它们很快):
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE c ALL date NULL NULL NULL 379553 Using where; Using temporary; Using filesort
服务器不使用日期索引,需要临时表和文件排序。任何想法?谢谢!
编辑:我将别名“date”更改为“ddmmyy”,结果是一样的。别名不是问题。
编辑 2:如果日期范围是两个月或更短,MySQL 使用日期索引(键)而不是“NULL”键