请建议索引以优化以下查询。我不允许重写查询,但创建索引:帮我优化此查询
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