这是查询,这里
al.activated_date
列有时间戳。我想按 application.id、application_logs.activated date 进行分组。
当我删除 DATE(al.activated_date) 时,查询执行时间为 1.2 秒。
但是当我使用它时,超过15秒..
SELECT
a.id, a.description, DATE(al.activated_date) as activated_date, a.category, COUNT(a.id) as total
FROM
application_logs al
JOIN
applications a ON al.behavior_id = a.id
JOIN
users u ON al.manager_id = u.id
WHERE
u.year_id = 3
AND
u.shift_id IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,16)
and u.is_active = 1
AND
a.system_id IN ("43","70","68","69","19","20","45","44","77","46","47","78","11","53","62","63","7","3","50","65","64","73","66","4","12","82","75","26","76","1","2","13","51","42","67","85","14","5","52","8","48","17","71","58","60","79","80","81","18","6","21","22","55","83","23","84","24","25","56","9","27","28","31","29","32","33","34","10","35","36","15","37","38","39","59","40","41","72","16")
AND
a.id IN (4500+ ids)
AND
DATE(al.activated_date) >= "2024-07-31"
AND
DATE(al.activated_date) <= "2025-05-30"
AND
a.category in ('aa','bb')
GROUP BY
a.id, DATE(al.activated_date);
请查看我的查询,并请分享一些建议或想法以获得相同的答复。
当您在列上调用函数时,无法使用列索引,因此会减慢查询速度。它必须扫描所有行,调用函数,然后进行比较,而不是使用索引进行过滤。
无需将时间戳转换为日期,只需与一天中的适当时间进行比较即可。
AND al.activated_date BETWEEN '2024-07-31 00:00:00' AND '2025-05-30 23:59:59'