【发布时间】:2017-11-10 12:25:46
【问题描述】:
我尝试使用以下查询在日期范围内吐出每一天并显示潜在客户、分配和回报的数量:
select
date_format(from_unixtime(date_created), '%m/%d/%Y') as date_format,
(select count(distinct(id_lead)) from lead_history where (date_format(from_unixtime(date_created), '%m/%d/%Y') = date_format) and (id_vertical in (2)) and (id_website in (3,8))) as leads,
(select count(id) from assignments where deleted=0 and (date_format(from_unixtime(date_assigned), '%m/%d/%Y') = date_format) and (id_vertical in (2)) and (id_website in (3,8))) as assignments,
(select count(id) from assignments where deleted=1 and (date_format(from_unixtime(date_deleted), '%m/%d/%Y') = date_format) and (id_vertical in (2)) and (id_website in (3,8))) as returns
from lead_history
where date_created between 1509494400 and 1512086399
group by date_format
date_created、date_assigned 和 date_deleted 字段是表示时间戳的整数。 id、id_lead、id_vertical 和 id_website 已编入索引。
将索引添加到 date_created、date_assigned、date_deleted 和 deleted 是否有助于加快速度?我遇到的问题是它非常慢,而且我不确定在使用 date_format(from_unixtime(... 时索引是否会有所帮助
这里是EXPLAIN:
【问题讨论】:
-
INDEX(date_created)需要WHERE。
标签: mysql select indexing count