除了使用索引外,MySQL查询速度慢吗?

时间:2013-08-13 16:54:39

标签: mysql indexing subquery

我正在运行查询以查找与同一个表中的其他人相关联的行(特别是那些具有相同ddef索引的行,也就是说。除了svar之外的所有NOT NULL列都是相同的)。查询非常慢,我不明白为什么。

表格:

    CREATE TABLE `data_der_testing` (
        `def`       VARCHAR( 30 )     NOT NULL ,
        `cntry`     VARCHAR( 5 )      NOT NULL ,
        `var`       VARCHAR( 10 )     NOT NULL ,
        `type`      VARCHAR( 4 )      NOT NULL ,
        `svar`      VARCHAR( 5 )      NOT NULL ,
        `track`     INT ( 3 )         NOT NULL ,
        `year`      INT ( 5 )         NOT NULL ,
        `v1211`     TEXT,
        `v1212`     TEXT,
        `v1302`     TEXT,
        `v1304`     TEXT,
        `v1305`     TEXT,
        INDEX   ddef ( `cntry`, `var`, `type`, `track`, `year` ),
        UNIQUE  ( `def` ),
        UNIQUE  idb  ( `cntry`, `var`, `type`, `svar`, `track`, `year` )                        
    ) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;

表格中的数据包含约450k行:

    `def`: a combination of all other "NOT NULL" columns, which makes it unique
    `cntry`: 34 country codes
    `var`: 86 variable codes
    `type`: 2 type codes
    `svar`: 3 sub-variable codes
    `track`: 6 codes
    `year`: 32 year codes (99, 1980...2010)
    `v...`: the variable value I want to check and update if needed

SHOW INDEX的输出:

    data_der    1   ddef    1   cntry   A   410                 BTREE   
    data_der    1   ddef    2   var     A   18871               BTREE   
    data_der    1   ddef    3   type    A   33388               BTREE   
    data_der    1   ddef    4   track   A   43404               BTREE   
    data_der    1   ddef    5   year    A   434048              BTREE

查询:

    SELECT *
    FROM `data_der`
    WHERE `type`='str' && `svar` != '99' &&`v1305` = '-90' && ROW(`cntry`,`var`,`type`,`track`,`year`) IN
    (
         SELECT `cntry`,`var`,`type`,`track`,`year`
         FROM `data_der`
         WHERE `type` = 'str' && `svar` != '99' && `v1305` != '-90'
         GROUP BY `cntry`,`var`,`type`,`track`,`year`
    )

这是解释:

    1   PRIMARY             data_der    ALL                        434048   Using where
    2   DEPENDENT SUBQUERY  data_der    index           ddef    71      6   Using where

如果您对如何使这个问题对普通受众更有帮助有任何建议,请告诉我。我一直从其他问题中获利,并乐意为此做出贡献。

最佳

Chonez

1 个答案:

答案 0 :(得分:0)

基于评论(Thx,人们!),我写了以下JOIN,我将用它来更新:

    UPDATE `data_der`
    SET `v1305` = '-95'
    WHERE `def` IN
    (
        SELECT * FROM
        (
            SELECT
                t1.`def`
            FROM `data_der` AS t1
            JOIN `data_der` AS t2
            ON (t1.`cntry`,t1.`var`,t1.`type`,t1.`track`,t1.`year`) = (t2.`cntry`,t2.`var`,t2.`type`,t2.`track`,t2.`year`)
            WHERE
                t1.`type`      = 'str'
                && t1.`svar`  != '99'
                && t1.`v1305`  = '-90'
                && t2.`v1305` != '-90'
        ) AS sub
    )

子查询的EXPLAIN:

    1   SIMPLE  t1  ref idb,sdef,cntry,var,type,track,year,ddef type    14  const   65338   Using where
    1   SIMPLE  t2  ref idb,sdef,cntry,var,type,track,year,ddef ddef    71  impic.t1.cntry,impic.t1.var,const,impic.t1.track,impic.t1.year  1   Using where

所以我的INDEX ddefcntryvartypetrackyear)正在使用!成功! (你可以忽略其他指数,我知道并非所有指数都有意义......)

只是一个奖励:是否有更短的方式来编写ON子句?