我在Ubuntu和;Windows 10和我都很困惑,为什么这个查询运行得这么慢(大约15秒),而且没有利用空间索引.我使用的MBRContains几乎与MySQL文档11.4.11 Using Spatial Indexes中描述的完全一样.如果包含在POLYGON
(框)中,则表location_coordinate
有735k行POINT
类型的横向/纵向坐标搜索.
查询:
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)