-1

在 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

欢迎解决方案、建议、替代语法或技巧..

4

1 回答 1

0

如果该信封内有数千个点,...

  • 第一个查询 (no ORDER BY) 将选择它在该信封中找到的前 500 个。
  • 第二个查询(带有ORDER BY)必须找到信封中的所有点,计算每个点的距离,对结果进行排序,然后交付 500。

LIMIT这是一个关于有和没有的简单事实ORDER BYSPATIAL它与5.7无关。

请提供证据证明我的主张

SELECT COUNT(*) 
    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 ) ;
于 2016-04-27T06:38:41.977 回答