在t1

时间:2017-10-18 20:27:24

标签: postgresql distance postgis

我有两个表t1t2,每个表都有一个名为geography的{​​{1}}类型列,每个表都有一列pts_geog单位标识符。我想在id添加一列,计算 t1中有多少单位距离t1中任意给定点1000米的距离。两个表都相当大,每个表大约150000行。要计算t2中每个点到t1中每个点的距离然而导致非常昂贵的操作,所以我正在寻找一些关于我正在做什么的指导有任何希望。因为内存不足,我从来没能完成这个操作。我可以以某种方式拆分操作(t2沿另一个维度where),但我需要更多的帮助。以下是我想要使用的t1

select

建议的答案和解答:

select
    count(nullif(
        ST_DWithin(
        g1.pts_geog,
        g2.gts_geog,
        1000,
        false),
        false)) as close_1000
from
    t1 as g1,
    t2 as g2
where
    g1.pts_geog IS NOT NULL
    and
    g2.pts_geog IS NOT NULL
GROUP BY g1.id

版本输出:

airbnb=> EXPLAIN ANALYZE
airbnb-> SELECT t1.listing_id, count(*)
airbnb-> FROM paris as t1
airbnb-> JOIN airdna_property as t2
airbnb-> ON ST_DWithin( t1.pts_geog, t2.pts_geog,1000 )
airbnb-> WHERE t2.city='Paris'
airbnb-> group by t1.listing_id;
                                                                           QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------
 HashAggregate  (cost=1030317.33..1030386.39 rows=6906 width=8) (actual time=2802071.616..2802084.109 rows=54400 loops=1)
   Group Key: t1.listing_id
   ->  Nested Loop  (cost=0.41..1030282.80 rows=6906 width=8) (actual time=0.827..2604319.421 rows=785571807 loops=1)
         ->  Seq Scan on airdna_property t2  (cost=0.00..74893.44 rows=141004 width=56) (actual time=0.131..738.133 rows=141506 loops=1)
               Filter: (city = 'Paris'::text)
               Rows Removed by Filter: 400052
         ->  Index Scan using paris_pts_geog_idx on paris t1  (cost=0.41..6.77 rows=1 width=64) (actual time=0.133..17.865 rows=5552 loops=141506)
               Index Cond: (pts_geog && _st_expand(t2.pts_geog, '1000'::double precision))
               Filter: ((t2.pts_geog && _st_expand(pts_geog, '1000'::double precision)) AND _st_dwithin(pts_geog, t2.pts_geog, '1000'::double precision, true))
               Rows Removed by Filter: 3260
 Planning time: 0.197 ms
 Execution time: 2802086.005 ms

更新2

这是在按照建议创建索引之后。请注意,由于我添加了新数据,行数略有增加,但这仍然是问题的大小。这需要52分钟。它仍然在 version | postgis_version ----------------------------------------------------------------------------------------------------------+--------------------------------------- PostgreSQL 9.5.7 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005, 64-bit | 2.2 USE_GEOS=1 USE_PROJ=1 USE_STATS=1 上显示Seq Scan,我不明白:为什么不在那里进行索引扫描,因为我创建了一个?

city

1 个答案:

答案 0 :(得分:2)

您所做的只是选择计数,只需将该条款移出选择列表即可修改连接。

SELECT t1.id, count(*)
FROM t1
JOIN t2
  ON ST_DWithin( t1.pts_geog, t2.pts_geog, 1000 )
GROUP BY t1.id;

如果您需要一个ST_DWithin可以使用的索引,请运行此..

CREATE INDEX ON t1 USING gist (pts_geog);
CREATE INDEX ON t2 USING gist (pts_geog);
VACUUM ANALYZE t1;
VACUUM ANALYZE t2;

现在运行上面的SELECT查询。

更新2

您的计划显示您已对城市进行了seq扫描,因此请在城市上创建索引,然后我们会看到我们还能做些什么

CREATE INDEX ON airdna_property (city);
ANALYZE airdna_property;