【发布时间】:2017-06-24 15:27:48
【问题描述】:
SELECT pm.team,
pm.username,
count(DISTINCT if(offer_pm.revenue>0.0,adv.adv_name,NULL)) AS adv_count,
sum(offer_pm.click),
sum(offer_pm.install),
sum(offer_pm.revenue),
sum(offer_pm.cost)
FROM (pm pm
INNER JOIN offer_pm_adv_day offer_pm ON offer_pm.pm_id=pm.id)
INNER JOIN advertiser adv ON offer_pm.adv_id=adv.id
WHERE offer_pm.logdate>='2016-01-01'
AND offer_pm.logdate<='2017-01-01'
GROUP BY pm.team,
pm.username
如果有where条件offer_pm.logdate>='2016-01-01' and offer_pm.logdate<='2017-01-01'
下面是 sql 的解释。而且成本时间长(20s)
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: offer_pm
type: range
possible_keys: PRIMARY,adv_id_index,logdate_index
key: PRIMARY
key_len: 3
ref: NULL
rows: 713003
Extra: Using where; Using temporary; Using filesort -->Useing where
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: pm
type: eq_ref
possible_keys: PRIMARY,uniq_name_team
key: PRIMARY
key_len: 4
ref: summary_report_refactor2.offer_pm.pm_id
rows: 1
Extra: Using where
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: adv
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: summary_report_refactor2.offer_pm.adv_id
rows: 1
Extra: Using where
但是sql
SELECT pm.team,
pm.username,
count(DISTINCT if(offer_pm.revenue>0.0,adv.adv_name,NULL)) AS adv_count,
sum(offer_pm.click),
sum(offer_pm.install),
sum(offer_pm.revenue),
sum(offer_pm.cost)
FROM (pm pm
INNER JOIN offer_pm_adv_day offer_pm ON offer_pm.pm_id=pm.id)
INNER JOIN advertiser adv ON offer_pm.adv_id=adv.id
GROUP BY pm.team,
pm.username
which 不具备 where 条件。
花费时间为 2s 。解释是
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: pm
type: index
possible_keys: PRIMARY,uniq_name_team
key: uniq_name_team
key_len: 604
ref: NULL
rows: 91
Extra: Using index; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: offer_pm
type: ref
possible_keys: adv_id_index,pm_id_index
key: pm_id_index
key_len: 4
ref: summary_report_refactor2.pm.id
rows: 10042
Extra: Using index condition
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: adv
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: summary_report_refactor2.offer_pm.adv_id
rows: 1
Extra: Using where
顺便说一句,logdate 列上有一个索引。
【问题讨论】:
-
你有什么问题?
-
logdate 列是什么类型的属性?
-
请为表格提供
SHOW CREATE TABLE。 -
您真的要包含两个 1 月 1 日的日期吗?
-
这可能是
JOIN+GROUP BY的经典“explode-implode”问题。请注意,对于COUNT,您必须执行DISTINCT以避免它。SUMs是不是太大了?是映射1:many;如果有,是哪条路?
标签: mysql optimization explain