下面是我的查询:
EXPLAIN SELECT
SUM(rcr.impressions),
SUM(rcr.clicks) AS clicks,
cv.id AS cup_version_id,
cv.source_id as source_id,
rcr.date AS date,
adg.subproduct_type_id
FROM
report_custom_records AS rcr
JOIN report_ad_groups AS adg ON (rcr.group_id = adg.ID)
JOIN ad_campaigns AS cmp ON (adg.campaign_id = cmp.id)
JOIN campaign_creatives AS cre ON (rcr.creative_id = cre.id)
JOIN campaign_versions AS cv ON (cre.version_id = cv.id)
WHERE
cmp.business_id IN (-1,9126,102538)
AND adg.campaign_id IN (-1,870689,870696,884963,884964,902027,907809,914889,914893,925233,930390,930391,955423,955429,1004323,1004324,1021355,1021356,1078026,1078027)
AND cast(rcr.date as date) BETWEEN '2018-03-01' AND '2018-10-31'
AND ((adg.target_type LIKE "%Pre-Roll%" AND adg.subproduct_type_id IN (2, 4)) OR adg.subproduct_type_id IN (12, 16 , 10 ))
AND cv.source_id IS NULL
GROUP BY
cup_version_id, date
如果我将其与日期条件(在date之间使用)一起使用,它将以11644969条记录的处理返回说明输出。 但是,如果我不使用日期条件就使用它,它只会返回1584016条记录给我。
仅供参考: 日期字段具有日期数据类型 表与联接一起使用,但没有外键
表格:
create table report_custom_records
(
group_id varchar(100) default '' not null,
creative_id int default 0 not null,
tp_creative_id varchar(20) not null,
date date not null,
impressions int(8) not null,
clicks int(8) not null,
cost float(6, 2) not null,
tp_creative_source smallint(6) not null comment '0 = TD, 1 = DFA',
primary key (adgroup_id, tp_creative_id, creative_id, date)
);
我在桌子上有下面的索引,这会减慢查询速度吗?
create index adg_id
on report_creative_records (group_id, date);
create index group_id_2
on report_creative_records (group_id, creative_id, date);
create index group_id_3
on report_creative_records (group_id);
create index creative_id
on report_creative_records (creative_id);
create index date
on report_creative_records (date);