索引表似乎不会加速查询性能

indexed tables don't seem to accelerate query performance

我正在使用 postgres,但索引主题似乎也适合 sql

我有 2 个 table:

table答:

name, id, geom, value, desc

其中:

geom is type of geometry

geom is indexed as geom_index

table乙:

 lnn_name, geom

其中:

geom is type of geometry

geom is indexed as geom__lnn_index

我检查了以下查询的性能:

select desc, sum((st_distance(A.geom, B.geom)<300)::INT) as res
from tblA as A, tblB as B
where A.geom is not null
group by A.desc

无论是否有索引,计算查询的时间似乎都一样

为什么索引无助于减少计算查询的时间?

我希望 geom 以一种可以减少查询时间的方式保存(点之间的长距离将被自动忽略)

您的查询内容是:

  1. 在两个表之间创建笛卡尔积。
  2. 一列然后根据距离过滤此笛卡尔积。

但是,总体 查询没有进行任何重要的过滤。如果要使用索引,请尝试将条件放在 ON 子句中:

select desc, count(*) as res
from tblA A join
     tblB B
     on st_distance(A.geom, B.geom) < 300
where A.geom is not null
group by A.desc;

现在查询是说要在 join 数据 聚合之前过滤结果。