0

このような 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 行があります。

これは正常ですか、それともさらに最適化できますか?

4

2 に答える 2

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 ;
于 2013-05-17T16:44:13.517 に答える