pgsql识别两个同结构表的不同

本文介绍了一种使用pgsql的except操作来高效地对比两张结构相同的大型表,以找出数据变化的方法。这种方法避免了游标遍历带来的长时间运行和内存占用问题,结果保存在新的表result_table_get_tables_differences中,包含源表标识。
摘要由CSDN通过智能技术生成

平常的业务中,往往需要检查记录业务信息的表在不同时期发生变化的记录,从而针对更改的记录做出更好的决策。之前有用游标实现过两张同结构表的遍历对比,但针对上十万上百万的数据记录的表时,运行起来需要很长时间,并且会造成运行内存短时间占满的情况,现在采用except进行两表操作。代码如下:

create or replace function get_tables_differences
(in schema_n_1 text,in schema_n_2 text,in table_n_1 text,in table_n_2 text)
returns void
language plpgsql
as $$
begin
drop table if exists temp_table_1;
drop table if exists temp_table_2;
drop table if exists result_table_get_tables_differences;
execute format('create table temp_table_1 as select * from %I.%I limit(0)',schema_n_1,table_n_1);
execute format('create table temp_table_2 as select * from %I.%I limit(0)',schema_n_2,table_n_2);
alter table temp_table_1 add column if not exists from_where text default 'first_table';
alter table temp_table_2 add column if not exists from_where text default 'second_table';
execute format('insert into temp_table_1 (select *
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值