递归查询性能问题

Recursive query performance issues

我有一个来自 geonames 网站 (http://download.geonames.org/export/dump/) 的英国数据库转储。它由大约 60000 条记录组成。

table结构如下:

CREATE TABLE `geoname` (
   `geonameid` INT(11) NOT NULL,
   `name` VARCHAR(200) NULL DEFAULT NULL,
   `asciiname` VARCHAR(200) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
   `preferredname` VARCHAR(200) NULL DEFAULT NULL,
   `alternatenames` VARCHAR(10000) NULL DEFAULT NULL COLLATE `utf8_unicode_ci',
   `latitude` DECIMAL(10,7) NULL DEFAULT NULL,
   `longitude` DECIMAL(10,7) NULL DEFAULT NULL,
   `feature_class` CHAR(1) NULL DEFAULT NULL,
   `feature_code` VARCHAR(10) NULL DEFAULT NULL,
   `country_code` VARCHAR(2) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
   `cc2` VARCHAR(60) NULL DEFAULT NULL,
   `admin1` VARCHAR(20) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
   `admin2` VARCHAR(80) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
   `admin3` VARCHAR(20) NULL DEFAULT NULL,
   `admin4` VARCHAR(20) NULL DEFAULT NULL,
   `population` INT(11) NULL DEFAULT NULL,
   `elevation` INT(11) NULL DEFAULT NULL,
   `gtopo30` INT(11) NULL DEFAULT NULL,
   `timezone` VARCHAR(40) NULL DEFAULT NULL,
   `moddate` DATETIME NULL DEFAULT NULL,
   PRIMARY KEY (`geonameid`),
   INDEX `geoname_name_idx` (`name`),
   INDEX `geoname_preferredname_idx` (`preferredname`),
   INDEX `geoname_admin1_idx` (`admin1`),
   INDEX `geoname_admin2_idx` (`admin2`),
   INDEX `geoname_admin3_idx` (`admin3`),
   INDEX `geoname_admin4_idx` (`admin4`),
   INDEX `geoname_feature_code_idx` (`feature_code`),
   INDEX `geoname_feature_class_idx` (`feature_class`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;

我已经为要在查询中使用的列添加了索引。该查询用于自动完成功能,但执行时间很长 - 下面的查询花费了 26.72 秒,这对于自动完成功能来说非常差:

mysql> SELECT t0.preferredname,
->        t4.preferredname AS town,
->        t3.preferredname AS county,
->        t2.preferredname AS district,
->        t1.preferredname AS admin1,
->        MIN(t0.geonameid)
-> FROM geoname t0
-> LEFT JOIN geoname t1 ON t1.admin1 = t0.admin1 AND t1.feature_code = 'ADM1'
-> LEFT JOIN geoname t2 ON t2.admin2 = t0.admin2 AND t2.feature_code = 'ADM2'
-> LEFT JOIN geoname t3 ON t3.admin3 = t0.admin3 AND t3.feature_code = 'ADM3'
-> LEFT JOIN geoname t4 ON t4.admin4 = t0.admin4 AND t4.feature_code = 'ADM4'
-> WHERE t0.feature_class IN ('P', 'A')
->   AND t0.preferredname LIKE 'preston%'
-> GROUP BY t0.preferredname,
->          t4.preferredname,
->          t3.preferredname,
->          t2.preferredname,
->          t1.preferredname;
+------------------------------+--------------------+--------------------------------+---------------------+----------+-------------------+
| preferredname                | town               | county                         | district            | admin1   | MIN(t0.geonameid) |
+------------------------------+--------------------+--------------------------------+---------------------+----------+-------------------+
| Preston                      | NULL               | Ellingham                      | Northumberland      | England  |           2639911 |
| Preston                      | NULL               | Preston                        | District of Rutland | England  |           2639914 |
| Preston                      | NULL               | Preston                        | East Yorkshire      | England  |           2639913 |
| Preston                      | NULL               | Preston District               | Lancashire          | England  |           2639912 |
| Preston                      | NULL               | Weymouth and Portland District | Dorset              | England  |           2639922 |
| Preston                      | Dymock             | Forest of Dean District        | Gloucestershire     | England  |           2639916 |
| Preston                      | Preston            | Cotswold District              | Gloucestershire     | England  |           2639918 |
| Preston                      | Preston            | Dover District                 | Kent                | England  |           2639920 |
| Preston                      | Preston            | North Hertfordshire District   | Hertfordshire       | England  |           2639917 |
| Preston Bagot                | Preston Bagot      | Stratford-on-Avon District     | Warwickshire        | England  |           2639910 |
| Preston Bisset               | Preston Bissett    | Aylesbury Vale                 | Buckinghamshire     | England  |           2639909 |
| Preston Bissett              | Preston Bissett    | Aylesbury Vale                 | Buckinghamshire     | England  |           7299788 |
| Preston Brook                | NULL               | Preston Brook                  | Borough of Halton   | England  |           7296534 |
| Preston Candover             | Preston Candover   | Basingstoke and Deane District | Hampshire           | England  |           2639908 |
| Preston Capes                | Preston Capes      | Daventry District              | Northamptonshire    | England  |           2639907 |
| Preston District             | NULL               | Preston District               | Lancashire          | England  |           7290581 |
| Preston Gubbals              | NULL               | Pimhill                        | Shropshire          | England  |           2639906 |
| Preston on Stour             | Preston on Stour   | Stratford-on-Avon District     | Warwickshire        | England  |           7299630 |
| Preston on the Hill          | NULL               | Preston Brook                  | Borough of Halton   | England  |           2639904 |
| Preston on Wye               | NULL               | Preston on Wye                 | Herefordshire       | England  |           2639903 |
| Preston Park                 | NULL               | NULL                           | Brighton and Hove   | England  |           2639921 |
| Preston Patrick              | Preston Patrick    | South Lakeland District        | Cumbria             | England  |           7298113 |
| Preston Richard              | Preston Richard    | South Lakeland District        | Cumbria             | England  |           7300167 |
| Preston Road                 | NULL               | Brent                          | Greater London      | England  |           2639919 |
| Preston St Mary              | Preston St. Mary   | Babergh District               | Suffolk             | England  |           2639915 |
| Preston St. Mary             | Preston St. Mary   | Babergh District               | Suffolk             | England  |           7301329 |
| Preston upon the Weald Moors | NULL               | Preston upon the Weald Moors   | Telford and Wrekin  | England  |           2639900 |
| Preston Wynne                | NULL               | Preston Wynne                  | Herefordshire       | England  |           2639899 |
| Preston-on-Tees              | NULL               | Preston-on-Tees                | Stockton-on-Tees    | England  |           7299560 |
| Preston-under-Scar           | Preston-under-Scar | Richmondshire District         | North Yorkshire     | England  |           7291664 |
| Prestonpans                  | NULL               | NULL                           | East Lothian        | Scotland |           2639902 |
+------------------------------+--------------------+--------------------------------+---------------------+----------+-------------------+
31 rows in set (26.72 sec)

mysql>

当我在上述查询中使用探查器时,我得到以下信息:

 mysql> select substring_index(event_name,'/',-1) as Status, truncate((timer_end-timer_start)/1000000000000,6) as Duration from performance_schema.events_stages_history_long where event_id>=8215932 and event_id<=9810811;
+----------------------+-----------+
| Status               | Duration  |
+----------------------+-----------+
| starting             |  0.000198 |
| checking permissions |  0.000004 |
| checking permissions |  0.000001 |
| checking permissions |  0.000001 |
| checking permissions |  0.000001 |
| checking permissions |  0.000005 |
| Opening tables       |  0.000044 |
| init                 |  0.000088 |
| System lock          |  0.000013 |
| optimizing           |  0.000022 |
| statistics           |  0.075318 |
| preparing            |  0.000059 |
| Creating tmp table   |  0.000082 |
| Sorting result       |  0.000014 |
| executing            |  0.000003 |
| Sending data         | 24.472337 |
| Creating sort index  |  0.000292 |
| end                  |  0.000007 |
| query end            |  0.000022 |
| removing tmp table   |  0.000118 |
| closing tables       |  0.000024 |
| freeing items        |  0.000278 |
| cleaning up          |  0.000001 |
+----------------------+-----------+
23 rows in set (0.00 sec)

当 运行 使用 Explain 的查询时,我得到以下信息:

mysql> EXPLAIN SELECT t0.preferredname,
->     t4.preferredname AS town,
->     t3.preferredname AS county,
->     t2.preferredname AS district,
->     t1.preferredname AS admin1,
->     MIN(t0.geonameid)
-> FROM geoname t0
-> LEFT JOIN geoname t1 ON t1.admin1 = t0.admin1 AND t1.feature_code = 'ADM1'
-> LEFT JOIN geoname t2 ON t2.admin2 = t0.admin2 AND t2.feature_code = 'ADM2'
-> LEFT JOIN geoname t3 ON t3.admin3 = t0.admin3 AND t3.feature_code = 'ADM3'
-> LEFT JOIN geoname t4 ON t4.admin4 = t0.admin4 AND t4.feature_code = 'ADM4'
-> WHERE t0.feature_class IN ('P', 'A')
->   AND t0.preferredname LIKE 'preston%'
-> GROUP BY t0.preferredname,
->          t4.preferredname,
->          t3.preferredname,
->          t2.preferredname,
->          t1.preferredname;
+----+-------------+-------+------------+-------+-----------------------------------------------------+---------------------------+---------+----------------------+------+----------+---------------------------------------------------------------------+
| id | select_type | table | partitions | type  | possible_keys                                       | key                       | key_len | ref                  | rows | filtered | Extra                                                               |
+----+-------------+-------+------------+-------+-----------------------------------------------------+---------------------------+---------+----------------------+------+----------+---------------------------------------------------------------------+
|  1 | SIMPLE      | t0    | NULL       | range | geoname_preferredname_idx,geoname_feature_class_idx | geoname_preferredname_idx | 603     | NULL                 |   55 |    70.01 | Using index condition; Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | t1    | NULL       | ref   | geoname_admin1_idx,geoname_feature_code_idx         | geoname_feature_code_idx  | 33      | const                |    4 |   100.00 | Using where                                                         |
|  1 | SIMPLE      | t2    | NULL       | ref   | geoname_admin2_idx,geoname_feature_code_idx         | geoname_feature_code_idx  | 33      | const                |  185 |   100.00 | Using where                                                         |
|  1 | SIMPLE      | t3    | NULL       | ref   | geoname_admin3_idx,geoname_feature_code_idx         | geoname_admin3_idx        | 63      | test.t0.admin3       |   14 |   100.00 | Using where                                                         |
|  1 | SIMPLE      | t4    | NULL       | ref   | geoname_admin4_idx,geoname_feature_code_idx         | geoname_admin4_idx        | 63      | test.t0.admin4       |    7 |   100.00 | Using where                                                         |
+----+-------------+-------+------------+-------+-----------------------------------------------------+---------------------------+---------+----------------------+------+----------+---------------------------------------------------------------------+
5 rows in set, 1 warning (0.06 sec)

请注意,我使用的是 group by 子句,因为数据的子级别名称重复。

如何优化此查询?任何建议提示和技巧将不胜感激。

我猜你希望检索匹配用户提供的不完整搜索字符串的地点,然后加入行政管辖区以提供信息更丰富的自动完成功能。

这里的技巧是快速检索候选地点。像这样的子查询就可以了。

              SELECT geonameid, preferredname, admin1, admin2, admin3, admin4
                FROM geonames
               WHERE feature_class IN ('P', 'A')
                 AND preferredname LIKE 'preston%'

这是查找操作的核心。它可以通过

上的复合覆盖索引来加速
CREATE INDEX lookup1 
     ON  geonames(feature_class, preferredname, admin1, admin2, admin3, admin4);

试试这个查询。看看它对你来说是否足够快(亚秒级)。如果不是,请尝试此变体:

              SELECT geonameid, preferredname, admin1, admin2, admin3, admin4
                FROM geonames
               WHERE feature_class ='P'
                 AND preferredname LIKE 'preston%'
              UNION ALL
              SELECT geonameid, preferredname, admin1, admin2, admin3, admin4
                FROM geonames
               WHERE feature_class ='A'
                 AND preferredname LIKE 'preston%'

MySQL 的查询规划器可以随机访问索引到第一个符合条件的行,然后通过顺序扫描索引检索所需的所有内容。

然后,您在 JOIN 操作中使用该子查询的结果集。现在,您只需处理联接中少量的相关行,而不是整个混乱。

SELECT t0.preferredname,
       t4.preferredname AS town,
       t3.preferredname AS county,
       t2.preferredname AS district,
       t1.preferredname AS admin1,
       MIN(t0.geonameid) geonameid
  FROM (
              SELECT geonameid, preferredname, admin1, admin2, admin3, admin4
                FROM geonames
               WHERE feature_class IN ='P'
                 AND preferredname LIKE 'preston%'
              UNION ALL
              SELECT geonameid, preferredname, admin1, admin2, admin3, admin4
                FROM geonames
               WHERE feature_class ='A'
                 AND preferredname LIKE 'preston%'
            ) t0
  LEFT JOIN geoname t1 ON t1.admin1 = t0.admin1 AND t1.feature_code = 'ADM1'
  LEFT JOIN geoname t2 ON t2.admin2 = t0.admin2 AND t2.feature_code = 'ADM2'
  LEFT JOIN geoname t3 ON t3.admin3 = t0.admin3 AND t3.feature_code = 'ADM3'
  LEFT JOIN geoname t4 ON t4.admin4 = t0.admin4 AND t4.feature_code = 'ADM4'
 GROUP BY t0.preferredname,
          t4.preferredname,
          t3.preferredname,
          t2.preferredname,
          t1.preferredname

专业提示:很多单列索引很少能加速具有多个过滤条件的查询,尤其是使用范围过滤器,例如LIKE 'something%'。适当的多列索引更有帮助。

我认为你应该先改变你的 table。

asciinamefeature_classfeature_codecountry_codecc2adminNtimezone 的排序规则应该更改为 latin1_general_ci。这将减少数据和索引的数据存储要求,并允许服务器在执行查询时在缓冲区中容纳更多数据。

您必须将 population 数据类型更改为 INTEGER UNSIGNED,因为当前您的数据可能已针对某些记录进行了截断(检查 'Commonwealth of Nations' 的值)。 您还可以考虑将 moddate 更改为 DATE,将 elevationgtopo30 更改为 SMALLINT 以进一步减少存储需求。

然后您需要将索引 geoname_admin1_idx 更改为:

INDEX `geoname_admin1_idx` (`admin1`)

至:

INDEX `geoname_admin1_idx` (`feature_code`, `admin1`)

对其他 geoname_adminN_idx 索引执行相同的操作。 这将允许服务器更快地在查询中进行连接。

仅这些更改就产生了巨大的差异,并且在不修改查询的情况下将我系统上的查询执行时间从 8 秒减少到几乎为零(0.1 秒)。

解释这些修改后的结果:

mysql> explain
    -> SELECT t0.preferredname,
    ->         t4.preferredname AS town,
    ->         t3.preferredname AS county,
    ->         t2.preferredname AS district,
    ->         t1.preferredname AS admin1
    ->         , MIN(t0.geonameid)
    ->  FROM geoname t0
    ->  LEFT JOIN geoname t1 ON t1.admin1 = t0.admin1 AND t1.feature_code = 'ADM1'
    ->  LEFT JOIN geoname t2 ON t2.admin2 = t0.admin2 AND t2.feature_code = 'ADM2'
    ->  LEFT JOIN geoname t3 ON t3.admin3 = t0.admin3 AND t3.feature_code = 'ADM3'
    ->  LEFT JOIN geoname t4 ON t4.admin4 = t0.admin4 AND t4.feature_code = 'ADM4'
    ->  WHERE t0.feature_class IN ('P', 'A')
    ->    AND t0.preferredname LIKE 'preston%'
    ->  GROUP BY t0.preferredname,
    ->           t4.preferredname,
    ->           t3.preferredname,
    ->           t2.preferredname,
    ->           t1.preferredname;
+----+-------------+-------+------------+-------+-----------------------------------------------------------------------------+---------------------------+---------+----------------------+------+----------+---------------------------------------------------------------------+
| id | select_type | table | partitions | type  | possible_keys                                                               | key                       | key_len | ref                  | rows | filtered | Extra                                                               |
+----+-------------+-------+------------+-------+-----------------------------------------------------------------------------+---------------------------+---------+----------------------+------+----------+---------------------------------------------------------------------+
|  1 | SIMPLE      | t0    | NULL       | range | geoname_preferredname_idx,geoname_feature_class_idx                         | geoname_preferredname_idx | 803     | NULL                 |   55 |    68.74 | Using index condition; Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | t1    | NULL       | ref   | geoname_admin1_idx,geoname_admin2_idx,geoname_admin3_idx,geoname_admin4_idx | geoname_admin2_idx        | 13      | const                |    4 |   100.00 | Using where                                                         |
|  1 | SIMPLE      | t2    | NULL       | ref   | geoname_admin1_idx,geoname_admin2_idx,geoname_admin3_idx,geoname_admin4_idx | geoname_admin2_idx        | 96      | const,test.t0.admin2 |   10 |   100.00 | NULL                                                                |
|  1 | SIMPLE      | t3    | NULL       | ref   | geoname_admin1_idx,geoname_admin2_idx,geoname_admin3_idx,geoname_admin4_idx | geoname_admin3_idx        | 36      | const,test.t0.admin3 |    4 |   100.00 | NULL                                                                |
|  1 | SIMPLE      | t4    | NULL       | ref   | geoname_admin1_idx,geoname_admin2_idx,geoname_admin3_idx,geoname_admin4_idx | geoname_admin4_idx        | 36      | const,test.t0.admin4 |    3 |   100.00 | NULL                                                                |
+----+-------------+-------+------------+-------+-----------------------------------------------------------------------------+---------------------------+---------+----------------------+------+----------+---------------------------------------------------------------------+

检查 key_len、ref、行和额外加入 tables。

您使用的查询也可能受益于索引 (feature_class, preferredname)。 这是用索引解释:

+----+-------------+-------+------------+-------+------------------------------------------------------------------------------------------------------+-----------------------------------------+---------+----------------------+------+----------+--------------------------------------------------------+
| id | select_type | table | partitions | type  | possible_keys                                                                                        | key                                     | key_len | ref                  | rows | filtered | Extra                                                  |
+----+-------------+-------+------------+-------+------------------------------------------------------------------------------------------------------+-----------------------------------------+---------+----------------------+------+----------+--------------------------------------------------------+
|  1 | SIMPLE      | t0    | NULL       | range | geoname_preferredname_idx,geoname_feature_class_idx,geoname_feature_class_preferredname_idx          | geoname_feature_class_preferredname_idx | 805     | NULL                 |   42 |   100.00 | Using index condition; Using temporary; Using filesort |
|  1 | SIMPLE      | t1    | NULL       | ref   | geoname_admin1_idx,geoname_admin2_idx,geoname_admin3_idx,geoname_admin4_idx,geoname_feature_code_idx | geoname_admin2_idx                      | 13      | const                |    4 |   100.00 | Using where                                            |
|  1 | SIMPLE      | t2    | NULL       | ref   | geoname_admin1_idx,geoname_admin2_idx,geoname_admin3_idx,geoname_admin4_idx,geoname_feature_code_idx | geoname_admin2_idx                      | 96      | const,test.t0.admin2 |   12 |   100.00 | NULL                                                   |
|  1 | SIMPLE      | t3    | NULL       | ref   | geoname_admin1_idx,geoname_admin2_idx,geoname_admin3_idx,geoname_admin4_idx,geoname_feature_code_idx | geoname_admin3_idx                      | 36      | const,test.t0.admin3 |    4 |   100.00 | NULL                                                   |
|  1 | SIMPLE      | t4    | NULL       | ref   | geoname_admin1_idx,geoname_admin2_idx,geoname_admin3_idx,geoname_admin4_idx,geoname_feature_code_idx | geoname_admin4_idx                      | 36      | const,test.t0.admin4 |    3 |   100.00 | NULL                                                   |
+----+-------------+-------+------------+-------+------------------------------------------------------------------------------------------------------+-----------------------------------------+---------+----------------------+------+----------+--------------------------------------------------------+