PostgreSQL和openGauss数据库优化器在merge join关联查询的SQL优化改写
看腻了文章就来听听视频讲解吧:https://www.bilibili.com/video/BV1oH4y137P7/
数据库类型 | 数据库版本 |
---|---|
PostgreSQL | 16.2 |
openGauss | 6.0 |
创建测试表和数据
drop table IF EXISTS t_test_1;
drop table IF EXISTS t_test_2;
create table t_test_1 (id int, info text);
insert into t_test_1 select generate_series(1,10000000),'testdb';
create table t_test_2 as select * from t_test_1;
create index idx_t_test_1_id on t_test_1(id);
create index idx_t_test_2_id on t_test_2(id);
vacuum analyze t_test_1;
vacuum analyze t_test_2;
复制
查询SQL
-- Merge Join
explain (analyze,buffers) select count(*) from t_test_1 t1 join t_test_2 t2 on (t1.id=t2.id) where t1.id between 5000000 and 5700000;
-- 等价写法
explain (analyze,buffers) select count(*) from t_test_1 t1 join t_test_2 t2 on (t1.id=t2.id) where t1.id between 5000000 and 5700000 and t2.id between 5000000 and 5700000;
复制
PostgreSQL 查询计划
测试 PostgreSQL 数据库版本为:16.2
-- 关闭并行,方便查看执行计划
set max_parallel_workers = 0;
set max_parallel_workers_per_gather = 0;
复制
从执行计划实际扫描的行数「 rows 」,可以看到 MergeJoin 会从索引开头全部扫描,直到超过匹配范围,即 t1 表根据where条件的between过滤走的索引扫描获取数据,t2 表从索引开头全部扫描到匹配范围。
openGauss 查询计划
测试 openGauss 数据库版本为:6.0
相比 PostgreSQL 的优化器,openGauss在这种场景的处理上相对智能点
拓展对比
PostgreSQL数据库调整where过滤条件行数后的执行计划:
-- Hash Join
explain (analyze,buffers) select count(*) from t_test_1 t1 join t_test_2 t2 on (t1.id=t2.id) where t1.id between 5000000 and 5500000;
-- 等价改写
explain (analyze,buffers) select count(*) from t_test_1 t1 join t_test_2 t2 on (t1.id=t2.id) where t1.id between 5000000 and 5500000 and t2.id between 5000000 and 5500000;
复制
-- Nested Loop
explain (analyze,buffers) select count(*) from t_test_1 t1 join t_test_2 t2 on (t1.id=t2.id) where t1.id between 5000000 and 5200000;
-- 等价改写
explain (analyze,buffers) select count(*) from t_test_1 t1 join t_test_2 t2 on (t1.id=t2.id) where t1.id between 5000000 and 5200000 and t2.id between 5000000 and 5200000;
复制
openGauss数据库调整where过滤条件行数后的执行计划:
-- PG Hash Join
explain (analyze,buffers) select count(*) from t_test_1 t1 join t_test_2 t2 on (t1.id=t2.id) where t1.id between 5000000 and 5500000;
-- PG Nested Loop
explain (analyze,buffers) select count(*) from t_test_1 t1 join t_test_2 t2 on (t1.id=t2.id) where t1.id between 5000000 and 5200000;
复制
随着where条件过滤数量的变化,PostgreSQL执行计划选择的方式会跟着有所变化,openGauss都是选择merge join的执行计划,按照merge join针对数据有序的场景到也算是正常
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
694次阅读
2025-04-03 15:21:16
明明想执行的SQL是DELETE、UPDATE,但为什么看到的是SELECT(FDW的实现原理解析)
小满未满、
395次阅读
2025-03-19 23:11:26
PostgreSQL初/中/高级认证考试(3.15)通过考生公示
开源软件联盟PostgreSQL分会
389次阅读
2025-03-20 09:50:36
9.9 分高危漏洞,尽快升级到 pgAdmin 4 v9.2 进行修复
严少安
299次阅读
2025-04-11 10:43:23
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
259次阅读
2025-04-15 14:48:05
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
241次阅读
2025-04-07 12:14:29
IvorySQL 4.4 发布 - 基于 PostgreSQL 17.4,增强平台支持
通讯员
231次阅读
2025-03-20 15:31:04
内蒙古公司成功完成新一代BOSS云原生系统割接上线
openGauss
209次阅读
2025-03-24 09:40:40
第4期 openGauss 中级认证OGCP直播班招生中!3月30日开课
墨天轮小教习
172次阅读
2025-03-17 15:48:40
PG vs MySQL 执行计划解读的异同点
进击的CJR
164次阅读
2025-03-21 10:50:08