Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 找到t2中距t1中所有点1000m范围内的所有点_Postgresql_Distance_Postgis - Fatal编程技术网

Postgresql 找到t2中距t1中所有点1000m范围内的所有点

Postgresql 找到t2中距t1中所有点1000m范围内的所有点,postgresql,distance,postgis,Postgresql,Distance,Postgis,我有两个表,t1和t2,每个表都有一个名为pts\u geog的geography类型列,每个表都有一个作为单元标识符的列id。我想在t1中添加一列,计算从t2到t1中任意给定点1000米范围内的单位数。两个表都相当大,每个表大约有150000行。计算t1中的每个点到t2中的每个点的距离会导致非常昂贵的操作,因此我正在寻找一些指导,以了解我正在做的事情是否有希望。由于内存不足,我一直无法完成此操作。我可以以某种方式拆分操作(使用where沿t1的另一个维度),但我需要更多帮助。下面是我想使用的选

我有两个表,
t1
t2
,每个表都有一个名为
pts\u geog
geography
类型列,每个表都有一个作为单元标识符的列
id
。我想在
t1
中添加一列,计算从
t2
到t1中任意给定点1000米范围内的单位数。两个表都相当大,每个表大约有150000行。计算
t1
中的每个点到
t2
中的每个点的距离会导致非常昂贵的操作,因此我正在寻找一些指导,以了解我正在做的事情是否有希望。由于内存不足,我一直无法完成此操作。我可以以某种方式拆分操作(使用
where
沿
t1
的另一个维度),但我需要更多帮助。下面是我想使用的
选择

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
版本输出:

                                                 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
更新2

这是在按照建议创建索引之后进行的。请注意,由于添加了新数据,行数略有增加,但问题的大小仍然相同。需要52分钟。它仍然在
city
上显示
Seq Scan
,我不明白:既然我创建了索引扫描,为什么它不在那里进行索引扫描呢

                                                                           QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------
 HashAggregate  (cost=904989.83..905049.21 rows=5938 width=8) (actual time=3118569.759..3118581.444 rows=54400 loops=1)
   Group Key: t1.listing_id
   ->  Nested Loop  (cost=0.41..904960.14 rows=5938 width=8) (actual time=2.624..2881694.755 rows=837837851 loops=1)
         ->  Seq Scan on airdna_property t2  (cost=0.00..74842.84 rows=121245 width=56) (actual time=2.263..949.073 rows=151018 loops=1)
               Filter: (city = 'Paris'::text)
               Rows Removed by Filter: 435564
         ->  Index Scan using paris_pts_geog_idx on paris t1  (cost=0.41..6.84 rows=1 width=64) (actual time=0.139..18.555 rows=5548 loops=151018)
               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: 3257
 Planning time: 0.377 ms
 Execution time: 3118583.203 ms
(12 rows)

您所做的只是选择计数,只需将子句从select列表中移出即可修剪联接

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 你的计划显示你对城市进行了序列扫描,所以在城市上创建一个索引,然后我们将看看我们还能做些什么

CREATE INDEX ON airdna_property (city);
ANALYZE airdna_property;

伯爵很奇怪。为什么不在where子句中使用距离计算,然后计算所有行数?粘贴
SELECT*FROM version(),postgis_version()的结果谢谢,看起来好多了。它已经运行了40分钟了。我会让它运行一夜。@FlorianOswald你有关于t1.pts_geog和t2.pts_geog的gist索引吗?你使用的是什么版本的postgis/postgresql?它应该很快。我恐怕不明白(接近初学者)。我肯定你不想把我的数据复制到要点上,所以你必须解释一下怎么做。我感觉这应该快得多,我在做一些愚蠢的事情,所以非常感谢你的帮助。@FlorianOswald你的SRID是什么?我想距离参数就是基于这个。这可能就是为什么它进展如此缓慢,你可能在做N^2,因为1000个单位可能是巨大的。op已经说过,而且一直说这些都是已经输入的地理信息。他可能没有索引。他不知道要点是什么。