实时展示st_union结果集的玩法

0
分享 2023-12-27

今天碰到客户在PostgreSQL中使用view实时获取st_union的结果集,有点流数据库的感觉,挺妖也挺有意思,所以就玩了一会。

客户的需求很简单,就是有个面图层,需要根据某个识别字段把相关的面给union起来,为了能够实时,所以使用了view,但是该view无法用arcgis desktop中打开,下面是测试过程

 1. 一个面状测试数据

1.png


 2. 该图层的源信息如下

2.png


3. 我们按照市代码创建view进行union

3.png


4. 实用ArcMap打开

4.png


实际上view上面无法创建空间索引也没有办法使用实体表上的索引,所以数据量大了,大比例尺访问会有效率问题
要解决上述问题还需要实用表来进行存储,然后在相关空间字段上建空间索引来解决性能问题。但是存储在堆表上需要解决数据实时同步的问题
可以通过触发器实现实时同步过程

以下是测试过程
1. 创建union后的表以及相关索引
create table testxian_shidaima_table as select 市代码::int as objectid,st_union(shape) as shape from testxian group by 市代码;

create index on testxian_shidaima_table using gist(shape);
2. 创建相关的trigger

4.png
create or replace function testxian_trigger() 
returns trigger as $$
begin
if (TG_OP = 'INSERT' ) THEN
if new.市代码 is null then
delete from testxian_shidaima_table where objectid=new.市代码;
insert into testxian_shidaima_table select new.市代码::int,st_union(shape) from testxian where 市代码=new.市代码 group by 市代码;
end if;
return new;

elsif (TG_OP = 'UPDATE' ) THEN
delete from testxian_shidaima_table where objectid=new.市代码 or objectid=old.市代码;
insert into testxian_shidaima_table select new.市代码::int,st_union(shape) from testxian where 市代码=new.市代码 group by 市代码;
return new;
elsif (TG_OP = 'DELETE' ) THEN
delete from testxian_shidaima_table where objectid=old.市代码;
insert into testxian_shidaima_table select old.市代码::int,st_union(shape) from testxian where 市代码=old.市代码;
return old;
end if;
end;

$$
language plpgsql;


create or replace trigger testxian_trigger after insert or update or delete on testxian for each row execute function testxian_trigger();

0 个评论

要回复文章请先登录注册