如何解决将时间属性设置为 postgis 几何的 m 维或单独的属性
首先是基本版本信息:
psql (Postgresql) 12.7 (Ubuntu 12.7-1.pgdg18.04+1)
postgis | 3.1.1
我使用空间数据库的目的是快速查询specified time scope
和space boundary
内的GPS轨迹。目前我的数据基本信息如下: >
-- geometry table column (there are 50,000 rows in table mpart5w-wkt)
test=# \d "mpart5w-wkt"
Table "public.mpart5w-wkt"
Column | Type | Collation | Nullable | Default
-----------+----------------------------+-----------+----------+---------
driver_id | character varying | | |
order_id | character varying | | |
geom | geometry(LinestringM,4326) | | |
Indexes:
"mpart5w-wkt_driver_id_idx" btree (driver_id)
"mpart5w-wkt_geom_idx" gist (geom gist_geometry_ops_nd)
-- Meta info
test=# select * from geometry_columns where f_table_name='mpart5w-wkt';
f_table_catalog | f_table_schema | f_table_name | f_geometry_column | coord_dimension | srid | type
-----------------+----------------+--------------+-------------------+-----------------+------+-------------
test | public | mpart5w-wkt | geom | 3 | 4326 | LInesTRINGM
(1 row)
-- sample data: LInesTRING M (lon lat timestamp)
test=# select st_astext(geom) from "mpart5w-wkt" limit 1;
LInesTRING M (104.04538 30.70745 1538402919,104.04538 30.70744 1538402928,104.04537 30.70745 1538402938,104.04536 30.70743 1538402948,104.04537 30.7074 1538402958,...)
强调一下,geom
是 (LinestringM,4326) 的几何类型。 GIS索引已经建立在geom列上。
我查看了关于 multi-index 的官方手册,它表明我们可以使用 4D 运算符类获得一个 4D 维的 BRIN
索引:
CREATE INDEX [indexname] ON [tablename]
USING BRIN ([geome_col] brin_geometry_inclusion_ops_4d);
同时,我们可以使用以下语法获得几何类型的 n 维 gist
索引:
CREATE INDEX [indexname] ON [tablename] USING GIST ([geometryfield] gist_geometry_ops_nd);
所以,我想只要提供 ZM 维度,构建 4D gist
索引是有帮助的。
在 functions 的大部分描述中,“此函数支持 3d,不会删除 z-index。”提到了没有提到m-index,而我对m-index没有更多的想法。
毕竟,没有确凿的证据表明M维是否支持多索引以及如何在M维上使用多索引。
也许我应该像这样创建表,这样我就不需要再处理 M 维了?
create table "part5w-wkt"(
driver_id varchar,order_id varchar,geom geometry(Linestring,4326),min_time timestamp,max_time timestamp
);
-- example (both start_time and end_time are parameters)
select * from "mpart5w-wkt"
where st_intersects(
geom,ST_MakeEnvelope(104.067,30.657,104.083,30.671,4326)
) and (
(min_time < start_time and start_time < max_time)
or
(min_time < end_time and end_time < max_time)
)
第二个问题是如何使用带有边界框的2D-index?
毕竟,没有证据表明使用带有 gist 索引的 m 维比使用带有关于时间的单独属性的 2D 几何更方便。所以,我决定先做一个2D-index的测试。
-- test 1
explain analyze
select count(order_id) from "mpart5w-wkt"
where st_intersects(
st_force2d(geom),4326)
);
-- test 2
explain analyze
select count(order_id) from "mpart5w-wkt"
where st_intersects(
st_force2d(geom),st_geometryfromtext(
'polygon((104.067 30.671,104.083 30.671,104.083 30.657,104.067 30.657,104.067 30.671))',4326
)
);
-- test 3
explain analyze
select count(order_id) from "mpart5w-wkt"
where st_intersects(
st_force2d(geom),'SRID=4326;polygon((104.067 30.671,104.067 30.671))'::geometry
);
-- almost the same result
Finalize Aggregate (cost=547292.05..547292.06 rows=1 width=8) (actual time=817.698..824.482 rows=1 loops=1)
-> Gather (cost=547291.84..547292.05 rows=2 width=8) (actual time=817.380..824.451 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Partial Aggregate (cost=546291.84..546291.85 rows=1 width=8) (actual time=804.706..804.707 rows=1 loops=3)
-> Parallel Seq Scan on "mpart5w-wkt" (cost=0.00..546291.83 rows=2 width=19) (actual time=97.585..803.734 rows=4394 loops=3)
Filter: st_intersects(st_force2d(geom),'0103000020E610000001000000050000003F355EBA49045A40D578E92631A83E403F355EBA49045A40B29DEFA7C6AB3E405A643BDF4F055A40B29DEFA7C6AB3E405A643BDF4F055A40D578E92631A83E403F355EBA49045A40D578E92631A83E40'::geometry)
Rows Removed by Filter: 12272
Planning Time: 0.268 ms
JIT:
Functions: 17
Options: Inlining true,Optimization true,Expressions true,Deforming true
Timing: Generation 2.771 ms,Inlining 99.859 ms,Optimization 117.378 ms,Emission 74.123 ms,Total 294.132 ms
Execution Time: 825.745 ms
(14 rows)
然而,测试表明结果几乎相同,即使我做了许多不同的测试,空间索引也不起作用。删除 st_force2d() 函数会降低效率。
如果在有额外时间限制的情况下进行相同的工作,效率会降低。
顺便说一句,如果经常使用lon-lat边界框同时需要计算距离,我应该使用4326和3857中的哪一个作为SRID来存储GPS轨迹几何?
解决方法
你可以通过在索引创建中使用函数geom
来告诉索引已经使用ST_Force2D
作为2D对它的记录进行排序,这样数据库就不需要在查询中这样做时间:
CREATE INDEX idx_part5w_wkt_geom ON "part5w-wkt"
USING gist (ST_Force2D(geom) gist_geometry_ops_nd);
如果您只是省略 ST_Force2D
中的 CREATE INDEX
,只要您稍后也不在 WHERE
子句中使用它,它也会产生类似的效果。长话短说:列的索引方式和查询方式必须匹配,否则索引可能不会被使用。
演示:db<>fiddle
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。