【问题标题】:indexed tables don't seem to accelerate query performance索引表似乎不会加速查询性能
【发布时间】:2020-09-01 08:02:38
【问题描述】:

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

我有 2 张桌子:

表 A:

name, id, geom, value, desc

地点:

geom is type of geometry

geom is indexed as geom_index

表 B:

 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 以一种可以减少查询时间的方式保存(点之间的长距离将被自动忽略)

【问题讨论】:

  • "我希望 geom 以一种可以减少查询时间的方式保存..." 不,索引所做的只是以不同的顺序保存相同的信息。可能有数据库可以帮助计算距离,但这不是索引的任务,
  • 两件事 - 多少行?其次 - 查看查询计划,看看它们是否不同以及如何解决。
  • edit您的问题并添加使用explain (analyze, buffers, format text)生成的execution plan不是只是一个“简单”解释)为formatted text,并确保保留计划的缩进。粘贴文本,然后将``` 放在计划前一行和计划后一行。还请包括所有索引的完整 create index 语句。

标签: sql postgresql postgis


【解决方案1】:

您的查询是:

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

然而,overall 查询并没有进行任何重要的过滤。如果您希望使用索引,请尝试将条件放在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 数据的结果聚合之前。

【讨论】:

  • 是否可以在您的答案查询中添加不为空的 A.geom 的计数?
  • @user3668129 。 . .这将需要left join,而这反过来可能会导致索引出现问题。 left join 对查询性能有何影响?
猜你喜欢
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
  • 1970-01-01
  • 2017-02-07
  • 2014-03-17
  • 1970-01-01
相关资源
最近更新 更多