【问题标题】:Gist index in PostgreSQL only works on order, but not on where predicatePostgreSQL 中的 Gist 索引仅适用于 order,但不适用于 where 谓词
【发布时间】:2021-12-30 08:18:27
【问题描述】:

我有一个带有LatLon 列的普通表,其中包含一个对象位置在空间中的点,并在此列上创建了 GiST 索引:

create table "Machine"
(
    "MachineId" integer not null,
    "Name" text,
    "Description" text default ''::text,
    "LatLon" point default point((0)::double precision, (0)::double precision) not null
);

create index on "Machine" using gist("LatLon");

我有一个关于仅选择距具有固定坐标的点一定距离内的点的查询:

select * from "Machine" where "LatLon" <-> point(25.123456789,-60.123456789) < 100;

虽然对此类查询的解释显示索引在执行期间没有被使用:

Seq Scan on "Machine"  (cost=0.00..244762.46 rows=1753121 width=208)
  Filter: (("LatLon" <-> '(25.123456789,-60.123456789)'::point) < '100'::double precision)
  Rows Removed by Filter: 5259364

同时,对LatLon 列执行规范的order by 查询显示索引工作正常:

select * from "Machine" order by "LatLon" <-> point(25.123456789,-60.123456789);
Index Scan using "Machine_LatLon_idx" on "Machine"  (cost=0.41..1021907.70 rows=5259364 width=216)
  Order By: ("LatLon" <-> '(25.123456789,-60.123456789)'::point)

为什么 GiST 索引不适用于带有距离运算符 &lt;-&gt;where 语句?

【问题讨论】:

    标签: sql postgresql indexing


    【解决方案1】:

    因为有人实现了一个,而不是另一个。

    请注意,这不仅仅是一个运算符,也没有其他运算符配对(&lt;-&gt;&lt;,此处)使用索引。因此,您必须引入支持此类索引的基础架构以及此特定实现。

    如果你只想要一个带索引的接近的东西,你需要把距离拉到点来得到一个圆。然后你有一个很好的二元运算符来抓取索引:

    select * from "Machine" where "LatLon" <@ circle(point(25.123456789,-60.123456789),100);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2020-02-06
      • 1970-01-01
      相关资源
      最近更新 更多