簡體   English   中英

MySQL 8 - MBRContains 不使用空間索引

[英]MySQL 8 - Where MBRContains not using spatial index

我在 Ubuntu 和 Windows 10 上都使用 MySQL 8.0.29,我很困惑為什么這個查詢運行如此緩慢(約 15 秒)並且沒有使用空間索引。 我使用的 MBRContains 幾乎完全按照 MySQL 文檔中的描述: 11.4.11 Using Spatial Indexes location_coordinate有 735k 行POINT類型的緯度/經度坐標搜索(如果包含在POLYGON (框)中)。

查詢:

SELECT long_lat_id, ST_Latitude(long_lat), ST_Longitude(long_lat)
FROM location_coordinate
WHERE MBRContains(ST_GeomFromText(
    'POLYGON((
        40.79607446677 -73.919978196147, 
        40.70923553323 -73.919978196147, 
        40.70923553323 -74.034611803853, 
        40.79607446677 -74.034611803853, 
        40.79607446677 -73.919978196147))', 4326)
    , long_lat);

解釋查詢

*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: location_coordinate
   partitions: NULL
         type: ALL
possible_keys: long_lat
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 735118
     filtered: 100.00
        Extra: Using where
1 row in set, 1 warning (0.00 sec)

表定義和解釋

CREATE TABLE location_coordinate (
    long_lat_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
    long_lat POINT NOT NULL SRID 4326,  
    PRIMARY KEY (long_lat_id),
    SPATIAL INDEX (long_lat)
);


mysql> EXPLAIN location_coordinate;
+--------------+--------------------+------+-----+---------+----------------+
| Field        | Type               | Null | Key | Default | Extra          |
+--------------+--------------------+------+-----+---------+----------------+
| long_lat_id  | mediumint unsigned | NO   | PRI | NULL    | auto_increment |
| long_lat     | point              | NO   | MUL | NULL    |                |
+--------------+--------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

Rick James 的評論之后,我打算提交一份錯誤報告,但事實證明這是 MySQL 8.0.29 中公認的錯誤

來自https://bugs.mysql.com/bug.php?id=107320

MySQL 8.0.31 變更日志中記錄的修復如下:

 Upgrading to MySQL 8.0.29 led to issues with existing spatial indexes. The root cause of the problem was a change in how geographic area computations were performed by the included Boost library, which was upgraded to version 1.77.0 in MySQL 8.0.29. We fix this by ensuring that we accommodate the new method whenever such computations are performed.

關閉。

臨時解決方法是在查詢中使用FORCE INDEX ()函數。 這將查詢時間從 15 秒減少到 0.062 秒。

SELECT long_lat_id, ST_Latitude(long_lat), ST_Longitude(long_lat)
FROM location_coordinate FORCE INDEX(long_lat)
WHERE MBRContains(ST_GeomFromText(
    'POLYGON((
        40.79607446677 -73.919978196147, 
        40.70923553323 -73.919978196147, 
        40.70923553323 -74.034611803853, 
        40.79607446677 -74.034611803853, 
        40.79607446677 -73.919978196147))', 4326)
    , long_lat);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM