递归查询性能问题

时间:2019-01-22 20:56:50

标签: mysql query-optimization query-performance recursive-query

我从英国的地名网站(http://download.geonames.org/export/dump/)获得一个数据库转储。它包含大约60000条记录。

表结构如下:

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子句,因为数据在子级别具有重复的名称。

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

2 个答案:

答案 0 :(得分:3)

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

这里的技巧是快速检索候选位置。这样的子查询可以解决问题。

              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%'之类的范围过滤器时。适当的多列索引会更有帮助。

答案 1 :(得分:2)

我认为您应该先更改表格。

asciinamefeature_classfeature_codecountry_codecc2adminNtimezone的排序规则到latin1_general_ci。这样可以减少数据和索引的数据存储需求,并允许服务器在执行查询时将更多数据放入缓冲区。

您必须将population数据类型更改为INTEGER UNSIGNED (整数),因为当前某些记录的数据可能已被截断(检查“国家联合体”的值)。 您还可以考虑将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,行和其他有关联接表的信息。

您使用的查询也可能会受益于索引(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                                                   |
+----+-------------+-------+------------+-------+------------------------------------------------------------------------------------------------------+-----------------------------------------+---------+----------------------+------+----------+--------------------------------------------------------+