gpt4 book ai didi

MySQL 3个链接表的查询优化

转载 作者:行者123 更新时间:2023-11-29 13:51:38 25 4
gpt4 key购买 nike

我有一个查询经常出现在 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

查询的解释如下所示...

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 行,但解释表明它只查看 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 行。

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

最佳答案

我建议您在(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 ;

关于MySQL 3个链接表的查询优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16613358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com