在 geom 字段上使用地理空间索引的地理空间搜索中,当我想按距离排序时,我的时间查询异常增加,是否有替代语法来避免这种情况?注意我知道这篇文章:https ://explainextended.com/2011/02/11/late-row-lookups-innodb/ 但是这个技巧不能通过下面的查询来实现:
此查询无顺序需要 0.005s
SELECT
ST_Distance_Sphere(Point(2.34, 48.85), geom) as distance
FROM testgeo1
WHERE ST_Contains( ST_MakeEnvelope(
Point((2.34+(500/111)), (48.85+(500/111))),
Point((2.34-(500/111)), (48.85-(500/111)))
), geom )
LIMIT 500
解释 :
+----+-------------+----------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
| 1 | SIMPLE | testgeo1 | NULL | range | sp_index | sp_index | 34 | NULL | 2609 | 100.00 | Using where |
+----+-------------+----------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
这个带有 ORDER BY 的需要 0.16s
SELECT
ST_Distance_Sphere(Point(2.34, 48.85), geom) as distance
FROM testgeo1
WHERE ST_Contains( ST_MakeEnvelope(
Point((2.34+(500/111)), (48.85+(500/111))),
Point((2.34-(500/111)), (48.85-(500/111)))
), geom )
ORDER BY distance
LIMIT 500
欢迎解决方案、建议、替代语法或技巧..