针对3个链接表的MySQL查询优化

时间:2013-05-17 15:55:55

标签: mysql optimization join right-join

我有一个经常出现在mysqlslow.log中的查询,就像这样...

# Query_time: 1  Lock_time: 0  Rows_sent: 12  Rows_examined: 18040
SELECT url, classname, cr_class, HDBclsslnk.cr_cl 
FROM HDBclsslnk 
JOIN wpthillsdatabase
  ON hn_cl = hillnumber AND area = '01d' 
RIGHT JOIN ShortClassList 
  ON HDBclsslnk.cr_cl = ShortClassList.cr_class 
GROUP BY ShortClassList.cr_class 
ORDER BY NULL

查询的EXPLAIN看起来像这样......

id| select_type | table                 | type   | possible_keys                                         | key             | key_len | ref                              | rows | Extra
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 |  SIMPLE     |      ShortClassList   | ALL    | NULL                                                  | NULL            | NULL    | NULL                             | 12   | Using temporary
1 |  SIMPLE     |      HDBclsslnk       | ref    | crcl-hncl,hncl-crcl                                   | crcl-hncl       | 6       | haroldst.ShortClassList.cr_class | 332  | Using index
1 | SIMPLE      |  wpthillsdatabase     | eq_ref | PRIMARY,SortIndex1,hillnumber_only,hil_lat_lon,Are... | hillnumber_only | 8       | haroldst.HDBclsslnk.hn_cl        | 1    |      

我无法理解为什么它会检查18040行但是Explain建议它只查看12 x 332 x 1行?

仅供参考,3张桌子看起来像这样......

CREATE TABLE `wpthillsdatabase` (
  `hillnumber` varchar(6) NOT NULL default '',
  `wikipedia` varchar(100) NOT NULL default '',
  `hillname` varchar(100) NOT NULL default '',
  `meters` char(6) NOT NULL default '',
  `GridZN` char(2) NOT NULL default '',
  `GridEast` char(5) NOT NULL default '',
  `GridNorth` char(5) NOT NULL default '',
  `numeast` char(7) NOT NULL default '',
  `numnorth` char(7) NOT NULL default '',
  `areadecimal` decimal(5,2) NOT NULL default '0.00',
  `area` varchar(3) NOT NULL default '',
  `area2` varchar(100) NOT NULL default '',
  `maps` varchar(100) NOT NULL default '',
  `waypointname` char(6) character set latin1 collate latin1_bin NOT NULL default '',
  `latitude` decimal(10,8) NOT NULL default '0.00000000',
  `longitude` decimal(10,7) NOT NULL default '0.0000000',
  `area3` smallint(2) NOT NULL default '0',
  `dip` decimal(5,1) NOT NULL default '0.0' COMMENT 'col height (m)',
  `climbedbytotal` smallint(6) NOT NULL default '0',
  `trigID` varchar(6) NOT NULL default '',
  `trigEast` varchar(7) NOT NULL default '',
  `trigNorth` varchar(7) NOT NULL default '',
  `trigLat` varchar(10) NOT NULL default '',
  `trigLon` varchar(10) NOT NULL default '',
  `SummitInfo` varchar(290) NOT NULL default '',
  PRIMARY KEY  (`hillnumber`,`area3`),
  UNIQUE KEY `meters` (`meters`,`hillnumber`),
  UNIQUE KEY `SortIndex1` (`hillnumber`,`hillname`,`meters`),
  UNIQUE KEY `NearByHills` (`numeast`,`numnorth`,`hillnumber`),
  UNIQUE KEY `hillnumber_only` (`hillnumber`),
  UNIQUE KEY `Area3_Sort` (`area3`,`hillnumber`,`hillname`,`meters`),
  UNIQUE KEY `GirdZN_sort` (`GridZN`,`hillnumber`,`hillname`,`meters`),
  UNIQUE KEY `hil_lat_lon` (`hillnumber`,`latitude`,`longitude`),
  KEY `trigID` (`trigID`,`hillname`,`meters`),
  KEY `climbedbytotal` (`climbedbytotal`),
  KEY `hillname` (`hillname`,`meters`),
  KEY `area3` (`area3`,`hillnumber`),
  KEY `hillname_only` (`hillname`),
  KEY `area3_trigID` (`area3`,`trigID`),
  KEY `Area_text` (`area`,`area3`),
  KEY `dip_area3` (`dip`,`area3`),
  KEY `lat_lon` (`latitude`,`longitude`,`meters`),
  KEY `trigID_area3` (`trigID`,`area3`),
  KEY `numeast_north` (`numeast`,`numnorth`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

CREATE TABLE `ShortClassList` (
  `classID` mediumint(9) NOT NULL,
  `sortseq` mediumint(9) NOT NULL,
  `cr_class` varchar(6) NOT NULL COMMENT 'class ref - class',
  `classname` varchar(20) NOT NULL,
  `url` varchar(20) NOT NULL default '',
  `areaURL` varchar(10) NOT NULL default '',
  `areatext` varchar(10) NOT NULL default '',
  PRIMARY KEY  (`classname`),
  UNIQUE KEY `sortseq` (`sortseq`),
  UNIQUE KEY `cr-class` (`cr_class`),
  KEY `areaURL` (`areaURL`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

CREATE TABLE `HDBclsslnk` (
  `classlinkID` mediumint(9) NOT NULL auto_increment,
  `hn_cl` varchar(6) NOT NULL default '' COMMENT 'hill number - class link ',
  `cr_cl` varchar(4) NOT NULL default '' COMMENT 'class ref - class link',
  PRIMARY KEY  (`classlinkID`),
  UNIQUE KEY `crcl-hncl` (`cr_cl`,`hn_cl`),
  UNIQUE KEY `hncl-crcl` (`hn_cl`,`cr_cl`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

HDBclsslnk有22,229行,wpthillsdatabase有9334行,ShortClassList有12行。

这是正常的,还是可以进一步优化?

2 个答案:

答案 0 :(得分:1)

我建议您在(area, hillnumber)上添加索引:

ALTER TABLE wpthillsdatabase
  ADD INDEX area_hillnumber
    (area, hillnumber) ;

您也可以尝试重写:

SELECT s.url, s.classname, s.cr_class, 
       ( SELECT h.cr_cl 
         FROM HDBclsslnk AS h
           JOIN wpthillsdatabase AS w
             ON  h.hn_cl = w.hillnumber 
             AND w.area = '01d' 
         WHERE h.cr_cl = s.cr_class 
         LIMIT 1
       ) AS cr_cl
FROM ShortClassList AS s ;

和此:

SELECT s.url, s.classname, s.cr_class, t.cr_cl
FROM ShortClassList AS s 
  LEFT JOIN
    ( SELECT DISTINCT h.cr_cl 
      FROM HDBclsslnk AS h
        JOIN wpthillsdatabase AS w
          ON  h.hn_cl = w.hillnumber 
          AND w.area = '01d' 
    ) AS t
      ON t.cr_cl = s.cr_class ;

答案 1 :(得分:0)

你可以试试这个:

SELECT
    url, 
    classname, 
    cr_class
FROM 
    ShortClassList 
WHERE
    cr_class IN (
        SELECT
            cr_cl
        FROM 
            HDBclsslnk h
        WHERE
            hn_cl IN (
                SELECT
                    hillnumber
                FROM 
                    wpthillsdatabase
                WHERE
                    area = '01d'
            )
    )
GROUP BY
     cr_class
ORDER BY 
    NULL