大家好,我有大量的数据,我想执行SQL查询以从服务器获取数据。检索数据需要花费很多时间。请建议我如何优化此查询。这是我的代码:
SELECT count(*) as count,
DATE_FORMAT(date,'%d-%b-%y') as dateName
from myTable
where date BETWEEN UTC_TIMESTAMP() - INTERVAL 30 DAY AND UTC_TIMESTAMP()
and id=123
GROUP BY DAY(date)
order by date ASC
SELECT count(*) as count,
DATE_FORMAT(date,'%d-%b-%y') as dateName
from myTable, (select UTC_TIMESTAMP() - INTERVAL 30 DAY as start_date, UTC_TIMESTAMP() as end_date) dd
where date BETWEEN dd.start_date AND dd.end_date
and id=123
GROUP BY DAY(date)
order by date ASC;
尝试将日期定义为FROM中单独的子查询的字段,以避免为每个记录计算
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句