請建議索引以優化以下查詢。我不允許重寫查詢,但創建索引:幫我優化此查詢
SELECT
`ADV`.`inds` as `c0`,
sum(`ADVpost`.`clk`) as `m0`
FROM
(SELECT *
FROM advts
WHERE comp_id =
(SELECT comp_id
FROM comp
WHERE name = 'abc')) as `ADV`,
(SELECT dt_id,
comp_id,
b_id,
ad_id,
clk,
resp
FROM advts_post
WHERE comp_id =
(SELECT comp_id
FROM comp
WHERE name = 'abc')) as `ADVpost`
WHERE
`ADVpost`.`ad_id` = `ADV`.`ad_id`
GROUP BY
`ADV`.`inds`
ORDER BY
ISNULL(`ADV`.`inds`), `ADV`.`inds` ASC
用於查詢的解釋是:
select_type table type possible_keys Extra
PRIMARY <derived2> ALL null Using temporary; Using filesort
PRIMARY <derived4> ALL null Using where; Using join buffer
DERIVED ADVpost ALL null Using where
SUBQUERY comp ALL null Using where
DERIVED advts ALL null Using where
SUBQUERY comp ALL null Using where
現有指標如下:在
ADVpost > PRIMARY KEY (`dt_id`,`comp_id`,`b_id`,`ad_id`)
comp > PRIMARY KEY (`comp_id`)
advts > PRIMARY KEY (`ad_id`)
謝謝提前。
貌似嵌套查詢是不必要的,一對夫婦的加入與在'comp.name','advts.comp_id','advts_post.comp_id指標','advts_post.ad_id'和'advts.inds'就足夠了。 – Orbling 2011-05-29 12:25:07
@如果我正確地閱讀了查詢,可以在'advts_post.ad_id'上加上索引。 – Tadeck 2011-05-29 12:27:35
@Tadeck:我想你的意思是'advts.ad_id'(正如我列出的其他人)。我故意將它關閉,因爲它被列爲已有的「PRIMARY」。 – Orbling 2011-05-29 12:37:48