(我的)SQL优化问题

时间:2011-08-04 13:49:42

标签: mysql sql optimization indexing

我有一个SQL,执行时间超过一分钟,这对我来说太过分了,有人可以给出优化建议。

更新2 - 增加速度的一点修改的sql:

SELECT STRAIGHT_JOIN
kw.`id` ,  kw.`clientid` ,  kw.`day` ,  kw.`campaignid` ,  kw.`campaign` ,  kw.`adgroupid`,  kw.`adgroup` ,  kw.`matchtype` ,  
kw.`firstpagecpc` ,  kw.`keywordtext` ,  kw.`qualityscore` ,  kw.`maxcpc` ,  kw.`avgcpm` ,  kw.`status` ,  kw.`keywordtext` ,  
kw.`matchtype` ,

SUM( kw.`impressions` )                                                  AS impressions,
SUM(  kw.`clicks` )                                                      AS clicks,
SUM(  kw.`impressions` *  kw.`avgposition` ) / SUM(  kw.`impressions` )  AS avgposition, 
SUM(  kw.`cost` )                                                        AS cost,
(SUM(  kw.`clicks` ) / SUM(  kw.`impressions` ) *100)                    AS ctr,
(SUM(  kw.`cost` ) / SUM(  kw.`clicks` ))                                AS avgcpc,
IF(kw.maxcpc = 0,kw.maxcpc,ad.defaultmaxcpc)                             AS maxcpc

FROM  `keywordsreport`                                                   AS kw
JOIN  `adgroupreport`                                                    AS ad 
ON (kw.clientid = ad.clientid AND kw.`day` = ad.day)

WHERE  
kw.`campaignid` = '55347673'
AND kw.`clientid` = '6588826821'
AND kw.`day` BETWEEN '2011-07-01' AND '2011-07-31'
AND (kw.`adid` = '' OR kw.`adid` = '0')
GROUP BY kw.`keywordtext`, kw.`matchtype`

keywordsreport table indexes:

PRIMARY  PRIMARY 235232  id

Unique   UNIQUE  235232  adgroupid
                         day
                         keywordtext
                         matchtype
                         adid
INDEX    INDEX   16802   keywordtext
                          matchtype
                          clientid

CLIENTIDDAY  INDEX   6637 clientid
                          day

adgroupreport表索引:

PRIMARY          PRIMARY 1328238  id

CLIENTIDDAY  INDEX   6447     clientid
                                  day

更新3: 使用EXPLAIN进行示例查询:

    i  select_type  table   type    possible_keys                                      key                key_len   ref     rows    Extra
    1   SIMPLE      kw      range   CLIENTID,CLIENTIDDAY,ADIDDAY,CAMPAIGNCLIENTDAY     CAMPAIGNCLIENTDAY  211   NULL    350 Using where; Using temporary; Using filesort
    1   SIMPLE      ad      range   CLIENTIDDAY                                        CLIENTIDDAY        65    NULL    42464   Using where

在每个表格中:keywordsreport和adgroupreport可能有大约1 000 000条记录。

4 个答案:

答案 0 :(得分:0)

看起来您需要clientid和/ campaignid上的索引。坚韧而没有更多细节。

答案 1 :(得分:0)

由于您的where子句中没有adgroupid,因此无法使用kw表中的唯一索引。 在clientid,day

创建一个索引

答案 2 :(得分:0)

我会更进一步......客户可以拥有多个广告系列,因此您的索引应首先使用最小的条件。

我会有一个索引
( CampaignID, ClientId, Day )

然后只是为了匹配预期的优化索引,只需将WHERE子句调整为

WHERE
         kw.campaignid = '23213231232'
      AND kw.clientid = '$clientid'
      AND kw.`day` BETWEEN '2011-07-31' AND '2011-07-01'
      AND ( kw.adid = '' OR kw.adid = '0')

答案 3 :(得分:0)

简单查询优化,

员工表 它有两列,如 id Totalmarks(满分900分) 1 700 2 600 。 。 。 。 n。

SELECT * FROM Employee WHERE Totalmarks BETWEEN 300 AND 600

执行查询需要一些时间。

ALTER TABLE Totalmarks ADD INDEX ( Totalmarks  );
SELECT * FROM Employee  WHERE Totalmarks BETWEEN 300 AND 600;