此查询需要花费大量时间执行,大约24秒(如Codeigniter分析器中所示)。需要优化此查询。
SELECT count(Knock0.id) as `12am - 10:30am`, count(Knock1.id) as `10:30am - 12:30pm` ,count(Knock2.id) as `12:30pm - 2:30pm` ,count(Knock3.id) as `2:30pm - 4:30pm` ,count(Knock4.id) as `4:30pm - 6:30pm` ,count(Knock5.id) as `6:30pm - 9:00pm` ,count(Knock6.id) as `9pm - 11:59pm`
FROM `ecosure_knocks` AS `Knock`
LEFT JOIN ecosure_knocks AS Knock0 ON (((Knock.id = Knock0.id) AND (time(Knock0.created) > '00:00') AND (time(Knock0.created) < '10:30')))
LEFT JOIN ecosure_knocks AS Knock1 ON (((Knock.id = Knock1.id) AND (time(Knock1.created) > '10:30') AND (time(Knock1.created) < '12:30')))
LEFT JOIN ecosure_knocks AS Knock2 ON (((Knock.id = Knock2.id) AND (time(Knock2.created) > '12:30') AND (time(Knock2.created) < '14:30')))
LEFT JOIN ecosure_knocks AS Knock3 ON (((Knock.id = Knock3.id) AND (time(Knock3.created) > '14:30') AND (time(Knock3.created) < '16:30')))
LEFT JOIN ecosure_knocks AS Knock4 ON (((Knock.id = Knock4.id) AND (time(Knock4.created) > '16:30') AND (time(Knock4.created) < '18:30')))
LEFT JOIN ecosure_knocks AS Knock5 ON (((Knock.id = Knock5.id) AND (time(Knock5.created) > '18:30') AND (time(Knock5.created) < '21:00')))
LEFT JOIN ecosure_knocks AS Knock6 ON (((Knock.id = Knock6.id) AND (time(Knock6.created) > '21:00') AND (time(Knock6.created) < '00:00')))
Where 1 AND Knock.created >= '2013-01-01 09:00:00' AND Knock.created < '2015-01-15 22:00:00'
Knock.created
是datetime
列。
答案 0 :(得分:0)
无需自我加入。使用条件和
SELECT sum(time(created) between '00:00' AND '10:30') as `00:00-10:30`,
sum(time(created) between '10:30' AND '12:30') as `10:30-12:30`,
...
FROM `ecosure_knocks`
Where created between '2013-01-01' and '2015-01-15'