KingbaseES Hint 使用
前言:KingbaseES V8R6C4 之前版本hint 使用方法是与Postgresql 相同的,通过 pg_hint_plan扩展,支持在SQL中使用hint。由于该版本的hint只能放置于SQL开始处,无法对子查询单独使用hint,实际使用非常不方便。由于无法对子查询单独使用hint,对于父查询与子查询使用相同表别名的情况就无法使用hint。从V8R6C4版本开始,KingbaseES 在hint 使用方法上与oracle进行了兼容,hint 只允许放在 select 后面,同时对于子查询,支持使用单独的hint。
一、启用hint支持
V8R6C4之前版本:设置 shared_preload_libraries=‘sys_hint_plan’,重启后,出现参数sys_hint_plan.enable_hint,设置该参数为 on。
V8R6C4:直接设置 enable_hint = on。该版本已直接将hint功能集成到内核中,不需要设置 shared_preload_libraries=‘sys_hint_plan’。
二、hint位置不同
1、V8R6C4之前版本
hint 可以放置于select 前后,甚至explain 之前。具体看以下例子。
没使用hint时的执行计划:
test=# explain analyze select * from t1 where id=123456;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------
Index Scan using ind_t1_id on t1 (cost=0.42..8.44 rows=1 width=208) (actual time=0.021..0.022 rows=1 loops=1)
Index Cond: (id = 123456)
Planning Time: 0.124 ms
Execution Time: 0.038 ms
hint 位置很随意,可以支持以下3种方式:
test=# explain analyze /*+seqscan(t1)*/select * from t1 where id=123456;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on t1 (cost=0.00..8383.00 rows=1 width=208) (actual time=15.768..24.416 rows=1 loops=1)
Filter: (id = 123456)
Rows Removed by Filter: 199999
Planning Time: 0.223 ms
Execution Time: 24.446 ms
(5 rows) test=# explain analyze select/*+seqscan(t1)*/ * from t1 where id=123456;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on t1 (cost=0.00..8383.00 rows=1 width=208) (actual time=16.367..24.982 rows=1 loops=1)
Filter: (id = 123456)
Rows Removed by Filter: 199999
Planning Time: 0.123 ms
Execution Time: 25.000 ms
(5 rows) test=# /*+seqscan(t1)*/explain analyze select * from t1 where id=123456;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on t1 (cost=0.00..8383.00 rows=1 width=208) (actual time=15.967..26.650 rows=1 loops=1)
Filter: (id = 123456)
Rows Removed by Filter: 199999
Planning Time: 0.154 ms
Execution Time: 26.671 ms
(5 rows)
2、V8R6C4版本
hint 只能放置于select 后面,具体见以下例子。
test=# explain analyze select/*+seqscan(t1)*/ * from t1 where id=123456;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on t1 (cost=0.00..8383.00 rows=1 width=208) (actual time=36.196..55.491 rows=1 loops=1)
Filter: (id = 123456)
Rows Removed by Filter: 199999
Planning Time: 0.177 ms
Execution Time: 55.537 ms
(5 rows) test=# explain analyze/*+seqscan(t1)*/ select * from t1 where id=123456;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------
Index Scan using ind_t1_id on t1 (cost=0.42..8.44 rows=1 width=208) (actual time=0.173..0.174 rows=1 loops=1)
Index Cond: (id = 123456)
Planning Time: 0.495 ms
Execution Time: 0.291 ms
(4 rows)
三、V8R6C4支持子查询单独hint
V8R6C4之前的版本不支持子查询hint,所有hint 必须放在父查询。
--所有hint都必须放在父查询select后面,父查询的hint 对于子查询是有效的。
test=# explain select/*+indexscan(t2 ind_t2_id)*/ count(*) from t1 where exists (select id from t2 where t2.id=t1.id);
QUERY PLAN
----------------------------------------------------------------------------------------------
Aggregate (cost=25680.20..25680.21 rows=1 width=8)
-> Merge Semi Join (cost=1.36..25180.20 rows=200000 width=0)
Merge Cond: (t1.id = t2.id)
-> Index Only Scan using ind_t1_id on t1 (cost=0.42..11090.42 rows=200000 width=4)
-> Index Scan using ind_t2_id on t2 (cost=0.42..11090.42 rows=200000 width=4)
(5 rows) --子查询hint 不生效。
test=# explain select count(*) from t1 where exists (select/*+indexscan(t2 ind_t2_id)*/ id from t2 where t2.id=t1.id);
QUERY PLAN
----------------------------------------------------------------------------
Aggregate (cost=24644.00..24644.01 rows=1 width=8)
-> Hash Semi Join (cost=11165.00..24144.00 rows=200000 width=0)
Hash Cond: (t1.id = t2.id)
-> Seq Scan on t1 (cost=0.00..7883.00 rows=200000 width=4)
-> Hash (cost=7883.00..7883.00 rows=200000 width=4)
-> Seq Scan on t2 (cost=0.00..7883.00 rows=200000 width=4)
(6 rows)
V8R6C4 支持子查询使用单独的hint,具体见以下例子:
--不使用hint 情况,走全表访问。
test=# explain analyze select count(*) from t1 where exists (select id from t2 where t2.id=t1.id);
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=24644.00..24644.01 rows=1 width=8) (actual time=188.164..188.166 rows=1 loops=1)
-> Hash Semi Join (cost=11165.00..24144.00 rows=200000 width=0) (actual time=65.473..178.178 rows=200000 loops=1)
Hash Cond: (t1.id = t2.id)
-> Seq Scan on t1 (cost=0.00..7883.00 rows=200000 width=4) (actual time=0.007..30.990 rows=200000 loops=1)
-> Hash (cost=7883.00..7883.00 rows=200000 width=4) (actual time=65.243..65.243 rows=200000 loops=1)
Buckets: 131072 Batches: 4 Memory Usage: 2786kB
-> Seq Scan on t2 (cost=0.00..7883.00 rows=200000 width=4) (actual time=0.006..32.561 rows=200000 loops=1)
Planning Time: 0.183 ms
Execution Time: 188.219 ms
(9 rows) --子查询使用hint
test=# explain select count(*) from t1 where exists (select/*+indexscan(t2 ind_t2_id)*/ id from t2 where t2.id=t1.id);
QUERY PLAN
----------------------------------------------------------------------------------------------
Aggregate (cost=25680.61..25680.62 rows=1 width=8)
-> Merge Semi Join (cost=1.36..25180.61 rows=200000 width=0)
Merge Cond: (t1.id = t2.id)
-> Index Only Scan using ind_t1_id on t1 (cost=0.42..11090.42 rows=200000 width=4)
-> Index Scan using ind_t2_id on t2 (cost=0.42..11090.42 rows=200000 width=4)
(5 rows)
四、父查询hint对于子查询同样有效
--在父查询可以对子查询的表指定hint
test=# explain select/*+indexscan(t2 ind_t2_id)*/ count(*) from t1 where exists (select id from t2 where t2.id=t1.id);
QUERY PLAN
----------------------------------------------------------------------------------------------
Aggregate (cost=25680.61..25680.62 rows=1 width=8)
-> Merge Semi Join (cost=1.36..25180.61 rows=200000 width=0)
Merge Cond: (t1.id = t2.id)
-> Index Only Scan using ind_t1_id on t1 (cost=0.42..11090.42 rows=200000 width=4)
-> Index Scan using ind_t2_id on t2 (cost=0.42..11090.42 rows=200000 width=4)
(5 rows) --在父查询指定涉及子查询表的连接方式
test=# explain select/*+nestloop(t1 t2)*/ count(*) from t1 where exists (select id from t2 where t2.id=t1.id) ;
QUERY PLAN
-------------------------------------------------------------------------------------
Aggregate (cost=123619.00..123619.01 rows=1 width=8)
-> Nested Loop Semi Join (cost=0.42..123119.00 rows=200000 width=0)
-> Seq Scan on t1 (cost=0.00..7883.00 rows=200000 width=4)
-> Index Only Scan using ind_t2_id on t2 (cost=0.42..0.57 rows=1 width=4)
Index Cond: (id = t1.id)
(5 rows) --甚至在子查询也指定子表与父表的连接方式
test=# explain select count(*) from t1 where exists (select/*+nestloop(t1 t2)*/ id from t2 where t2.id=t1.id) ;
QUERY PLAN
-------------------------------------------------------------------------------------
Aggregate (cost=123619.00..123619.01 rows=1 width=8)
-> Nested Loop Semi Join (cost=0.42..123119.00 rows=200000 width=0)
-> Seq Scan on t1 (cost=0.00..7883.00 rows=200000 width=4)
-> Index Only Scan using ind_t2_id on t2 (cost=0.42..0.57 rows=1 width=4)
Index Cond: (id = t1.id)
(5 rows)
五、注意点
- hint 指定的多项的分隔符只能是空格。
- 可以不用创建 pg_hint_plan,该扩展插件实际提供 hint_plan.hints 表。
KingbaseES Hint 使用的更多相关文章
- KingbaseES V8R3集群运维案例之---主库系统down failover切换过程分析
案例说明: KingbaseES V8R3集群failover时两个cluster都会触发,但只有一个cluster会调用脚本去执行真正的切换流程,另一个有对应的打印,但不会调用脚本,只是走相关的 ...
- KingbaseES 局部索引
一个列要不要建立btree索引,判断条件是其键值分布是否够离散,比如主键.唯一键,可以建立索引.如果这个列有大量重复的值,则建立索引没有意义. 在生产环境中常会碰到键值分布不均匀的列,如表t1有一个名 ...
- KingbaseES V8R6集群管理运维案例之---repmgr standby switchover故障
案例说明: 在KingbaseES V8R6集群备库执行"repmgr standby switchover"时,切换失败,并且在执行过程中,伴随着"repmr stan ...
- KingbaseES V8R3集群管理维护案例之---集群迁移单实例架构
案例说明: 在生产中,需要将KingbaseES V8R3集群转换为单实例架构,可以采用以下方式快速完成集群架构的迁移. 适用版本: KingbaseES V8R3 当前数据库版本: TEST=# s ...
- KingbaseES V8R3集群管理和维护案例之---failover切换wal日志变化分析
案例说明: 本案例通过对KingbaseES V8R3集群failover切换过程进行观察,分析了主备库切换后wal日志的变化,对应用者了解KingbaseES V8R3(R6) failover ...
- KingbaseES V8R3集群维护案例之---在线添加备库管理节点
案例说明: 在KingbaseES V8R3主备流复制的集群中 ,一般有两个节点是集群的管理节点,分为master和standby:如对于一主二备的架构,其中有两个节点是管理节点,三个数据节点:管理节 ...
- KingbaseES应对表年龄增长过快导致事务回卷
背景 前几天碰到这样一个场景,在KingbaseES数据库当作数据同步节点.其特点是接收过来的数据量巨大,其更新超级频繁,最大的数据库达到6TB.这还不是主要的,主要导致问题发生原因是同步数据库有很多 ...
- Hint 使用--leading
Oracle hint -- leading 的作用是提示优化器某张表先访问,可以指定一张或多张表,当指定多张表时,表示按指定的顺序访问这几张表.而 Postgresql leading hint的功 ...
- KingbaseES V8R6单实例外部备份故障案例
案例说明: 在KingbaseES V8R6单实例环境,配置外部备份服务器使用sys_backup.sh物理备份时,出现以下"WAL segment xxx was not archived ...
随机推荐
- OpenSSF安全计划:SBOM将驱动软件供应链安全
在 软件成分分析(SCA)一文中,我们简单提到软件物料清单(SBOM)在安全实践中的价值. 本期文章将带你深入了解 "SBOM 无处不在"计划是什么,以及 SBOM 对未来软件供应 ...
- HTML:<input type="text"> 输入数字时的验证!(在提交时验证)
<!--非负数:<input type="text" name="" pattern="^\d+$">--> < ...
- Navicat中查询mysql版本
SELECT VERSION( ) FROM DUAL
- Linux YUM yum 命令详解
Yum命令 常用yum命令列表 command is one of: * install package1 [package2] [...] * update [package1] [package2 ...
- Golang并发编程——goroutine、channel、sync
并发与并行 并发和并行是有区别的,并发不等于并行. 并发 两个或多个事件在同一时间不同时间间隔发生.对应在Go中,就是指多个 goroutine 在单个CPU上的交替运行. 并行 两个或者多个事件在同 ...
- 常用的Linux命令和Git的必要配置
常用的Linux命令平时一定要多使用这些基础的命令! 1.cd : 改变目录. 2.cd . . 回退到上一个目录,直接cd进入默认目录 3.pwd : 显示当前所在的目录路径. 4.ls(ll): ...
- MIT 6.824 Lab2D Raft之日志压缩
书接上文Raft Part C | MIT 6.824 Lab2C Persistence. 实验准备 实验代码:git://g.csail.mit.edu/6.824-golabs-2021/src ...
- isNotBlank()方法和isNotEmpty()方法的区别
- angular 变化检测和ngZone
- PostgreSQL定时备份
# PostgreSQL定时备份 # 版本:PostgreSQL14.0 # 一.如果你在pg_hba.conf中设定了密码登录,那么你就需要设置一个默认密码共psql登录的时候跳过验证.如果是roo ...