KingbaseES 两表关联Update的两种写法与性能
熟悉oracle 的人都知道,对于两表的关联更新,其执行计划主要有 Filter 和 Outer Join 两种方式。对于大批量数据的update,Join方式明显是更优的选择。KingbaseES 和 Postgresql 也支持两种方式的关联update,语法上采用两种不同的写法。
以下以例子的形式展示两种写法及性能上的差异。这些例子同时通过KingbaseES V8R6和 Postgresql 12.3 环境验证。
一、准备测试数据
1 2 3 4 5 6 7 8 9 10 11 | create table t1(id1 integer ,name1 varchar (200)); create table t2(id2 integer ,name2 varchar (200)); insert into t1 select * from ( select generate_series(1,1000000),repeat( 'a' ,50)) as a order by random(); insert into t2 select * from ( select generate_series(1,1000000),repeat( 'b' ,50)) as a order by random(); create index ind_t1_id1 on t1(id1); create index ind_t2_id2 on t2(id2); analyze t1; analyze t2; |
二、性能测试
1、语法一
采用类似oracle filter 方式,逐条处理t1 表的每条记录。对于t1表的每条记录,都需要访问t2表。
1 2 3 4 5 6 7 8 9 10 11 | test=# explain analyze update t1 set name1=( select name2 from t2 where id1=id2); QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Update on t1 (cost=0.00..8462810.00 rows =1000000 width=428) (actual time =13072.720..13072.721 rows =0 loops=1) -> Seq Scan on t1 (cost=0.00..8462810.00 rows =1000000 width=428) (actual time =0.035..6620.732 rows =1000000 loops=1) SubPlan 1 -> Index Scan using ind_t2_id2 on t2 (cost=0.42..8.44 rows =1 width=51) (actual time =0.006..0.006 rows =1 loops=1000000) Index Cond: (id2 = t1.id1) Planning Time : 0.116 ms Execution Time : 13072.780 ms (7 rows ) |
2、语法二
采用hash join,大批量的update 效率更高。
1 2 3 4 5 6 7 8 9 10 11 12 | test=# explain analyze update t1 set name1=name2 from t2 where id1=id2; QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------- Update on t1 (cost=37693.00..98122.00 rows =1000000 width=67) (actual time =8197.309..8197.312 rows =0 loops=1) -> Hash Join (cost=37693.00..98122.00 rows =1000000 width=67) (actual time =349.817..1633.896 rows =1000000 loops=1) Hash Cond: (t2.id2 = t1.id1) -> Seq Scan on t2 (cost=0.00..20310.00 rows =1000000 width=61) (actual time =0.021..191.730 rows =1000000 loops=1) -> Hash (cost=20310.00..20310.00 rows =1000000 width=10) (actual time =348.798..348.798 rows =1000000 loops=1) Buckets: 131072 Batches: 16 Memory Usage: 3594kB -> Seq Scan on t1 (cost=0.00..20310.00 rows =1000000 width=10) (actual time =0.034..153.882 rows =1000000 loops=1) Planning Time : 0.780 ms Execution Time : 8197.543 ms |
三、结论
对于大批量数据update,基于hash join 的update方法效率上要高效很多。
KINGBASE研究院
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!