在PostgreSQL中有与SQLite R*Tree等价的东西吗?

tpgth1q7  于 5个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(73)

我正在将一个SQLite数据库结构迁移到PostgreSQL。这个SQLite数据库使用R*Tree模块和空间索引来快速搜索3D边界框。
我没有空间索引和PostgreSQL的经验。
我已经阅读了PostgreSQL GiST索引的文档,它似乎表明这可以实现类似的功能。我一直无法找到如何将此功能应用于3D边界框检查。
我想用<@操作符加上框和点应该可以做到这一点。有人能详细说明一下这种直觉是否正确,并提供一个小例子来说明如何定义一个包含空间3D数据的表和一个进行边界框检查的查询吗?
下面是我正在寻找的示例数据和查询想法:
| ID| X| y| z|
| --|--|--|--|
| 1 | 0 | 1 | 2 |
| 2 | 4 | 5 | 6 |
第一个月

uurity8g

uurity8g1#

使用PostGIS,您可以像其他GiST索引一样设置基于R树的最多4D索引。Demo at db<>fiddle:
用样品设置表:

create extension postgis;

create table business(
  id int generated by default as identity primary key,
  business_name text,
  x numeric, y numeric, z numeric, t numeric,
  geom geometry(PointZM,4326) generated always as 
      (st_setsrid(st_makepoint(x,y,z,t),4326)) stored);

select setseed(.4);
insert into business (business_name,x,y,z,t)
select 'business_'||n, x, y, z, t
from generate_series(1,1e5,1)a(n),
  lateral(select   n AS n1,
                   180-random()*360 x,
                   90 -random()*180 y, 
                   50 -random()*100 z,
                   50 -random()*100 t)b;

字符串
设置索引。默认操作符类只使用每个几何体的两个维度来构建r树,因此您需要指定gist_geometry_ops_nd操作符类来处理n维几何体的所有维度:

create index geom_idx on business using gist(geom gist_geometry_ops_nd);


一种测试操作,显示索引用途:选择最接近参考点的7个点:

prepare postgis_based as
select id,business_name,x,y,z,t
      ,(select geom from business where id=42)<<->>geom AS distance 
from business
order by distance
limit 7;

explain analyze execute postgis_based;
execute postgis_based;
QUERY PLAN
Limit  (cost=8.59..9.53 rows=7 width=73) (actual time=0.428..1.624 rows=7 loops=1)
  InitPlan 1 (returns $0)
    ->  Index Scan using business_pkey on business business_1  (cost=0.29..8.31 rows=1 width=48) (actual time=0.008..0.008 rows=1 loops=1)
          Index Cond: (id = 42)
  ->  Index Scan using geom_idx on business  (cost=0.28..13468.38 rows=100005 width=73) (actual time=0.427..1.622 rows=7 loops=1)
        Order By: (geom <<->> $0)
Planning Time: 0.351 ms
Execution Time: 1.689 ms

的字符串
| ID|企业名称|X| y| z|不|距离|
| --|--|--|--|--|--|--|
| 42 |商业_42| -91.8261216469166 |7.7997970642875| 2.93153014852416| 13.8754985934873| 0 |
| 39781 |业务_39781| -91.651074810554 |10.1567444370512| 1.08794511732091| 19.9058199716022| 6.734198096546056|
| 10212 |业务_10212| -96.0308356409243 |16780328827525| 6.99214194619289| 17.9070448007494| 285942385786669|
| 78354 |业务_78354| -94.2913320527778 |12.8802423990292| 5.95771554931872| 17.565112372697| 7.393188423419308|
| 82772 |业务_82772| -90.3963886636943 |13.9114309111029| -2.37759157752899 |15.9845495479146| 8.487112236283533|
| 47097 |企业_47097| -93.041286078567 |14.2177864450063| -1.89386518525991 |11.233109282625| 8.540133118659915|
| 94516 |业务_94516| -87.3058458168426 |1.38796268779309| 1.78446589911467| 18.3034143744421|电话:08111822448362|
如果在使用默认索引设置时,将xy清零,只填充zt,则可以使用observe slow sequential scans kicking in,这表明仅使用前两个维度来构建树,从而在这种情况下没有帮助。

相关问题