【发布时间】: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 索引不适用于带有距离运算符 <-> 的 where 语句?
【问题讨论】:
标签: sql postgresql indexing