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 ...
随机推荐
- 视图模板引擎——Vue【双向绑定】原理剖析
首先我们来了解一下MVC.MVP.MVMM这三大架构模式在前端角度上的理解. MVC分别是 Model(模型).View(视图).Controller(控制器)三个模块.View(视图层)最主要完成前 ...
- 建立QT工程的规范型,以及重要性
当前管理开发多个项目,故名Projects 下一级目录,具体项目,故示例Project,根据实际情况自行取名 再下一级目录,有三个子目录 bin:生成的可执行文件或者动态链接库,build:编译源码时 ...
- zabbix监控apache80端口
1.修改zabbix_agentd.conf 修改# EnableRemoteCommands=0 -->去掉注释修改为1--> EnableRemoteCommands=1 ###允许客 ...
- Pyinstaller打包pikepdf失败的问题排查
问题 最近在项目里用到了pikepdf这个库,用于实现pdf水印插入的一个小功能,源码调试阶段运行一切OK但是在出包时报了如下异常. Traceback (most recent call last) ...
- nextInt和nextLine以及next方法的区别
1.nextInt() 只读取整型的数据,输入读取完之后,光标仍在当前行. 2.nextLine() 扫描到一行内容,当遇见换行符时,结束扫描.一旦输入读取完毕,该方法会将光标移到下一行开始的位置. ...
- lerna学习笔记
lerna简介 Lerna 是一个优化基于Git+npm的多package项目的项目管理工具,lerna是架构优化的产物,项目复杂度提升后,架构优化的主要目标是以提高ROI为核心的 lerna的主要功 ...
- Collection集合和Collection集合常用功能
Collection集合常用功能 方法: boolean add(E e); 向集合中添加元素 boolean remove(E e); 删除集合中的某个元素 void clear(); 清空集合所有 ...
- idea java 打包的方法
方法1: 在pom.xml 里面加上maven打包的配置 <plugin> <groupId>org.springframework.cloud</groupId> ...
- Hadoop - MapReduce 过程
Hadoop - MapReduce 一.MapReduce设计理念 map--->映射 reduce--->归纳 mapreduce必须构建在hdfs之上的一种大数据离线计算框架 在线: ...
- 第k小 来源:牛客网
题目 链接:https://ac.nowcoder.com/acm/contest/28886/1003 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K, ...