MySQL 5.7 无法获取要使用的空间索引
MySQL 5.7 cannot get spatial index to be used
CREATE TABLE zip_polygons_new(
zipcode MEDIUMINT(5) UNSIGNED NOT NULL,
zip_polygon MULTIPOLYGON NOT NULL,
spatial index (zip_polygon)
);
EXPLAIN
SELECT zipcode
FROM zip_polygons_new zp
WHERE ST_CONTAINS(zp.zip_polygon, ST_GEOMFROMTEXT('POINT(-99.24012 19.53285)'));
我总是得到:
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
zp
ALL
26376
100.0
Using where
我试过:
- InnoDB 和 MyIsam。
- 几个空间函数(例如 MBRCONTAINS,ST_CONTAINS)。
- 在 WHERE 子句的 RHS 上设置等于 1。
已检查:
MySQL 文档
Why Spatial index not used by MySQL?
我缺少什么才能使用索引?
事实证明我没有意识到我有 mysql@8 运行,当切换回 mysql@5.7 时索引现在被使用了。
所以问题出现在 mysql@8 上,因为我的问题与 mysql@5.7 有关,我认为它是自我回答的。可能的问题是 mysql@8 期望显式 SRID 使用空间索引,而 mysql@5.7 不期望。
CREATE TABLE zip_polygons_new(
zipcode MEDIUMINT(5) UNSIGNED NOT NULL,
zip_polygon MULTIPOLYGON NOT NULL,
spatial index (zip_polygon)
);
EXPLAIN
SELECT zipcode
FROM zip_polygons_new zp
WHERE ST_CONTAINS(zp.zip_polygon, ST_GEOMFROMTEXT('POINT(-99.24012 19.53285)'));
我总是得到:
id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | SIMPLE | zp | ALL | 26376 | 100.0 | Using where |
我试过:
- InnoDB 和 MyIsam。
- 几个空间函数(例如 MBRCONTAINS,ST_CONTAINS)。
- 在 WHERE 子句的 RHS 上设置等于 1。
已检查:
MySQL 文档
我缺少什么才能使用索引?
事实证明我没有意识到我有 mysql@8 运行,当切换回 mysql@5.7 时索引现在被使用了。
所以问题出现在 mysql@8 上,因为我的问题与 mysql@5.7 有关,我认为它是自我回答的。可能的问题是 mysql@8 期望显式 SRID 使用空间索引,而 mysql@5.7 不期望。