前言: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)

五、注意点

  1. hint 指定的多项的分隔符只能是空格。
  2. 可以不用创建 pg_hint_plan,该扩展插件实际提供 hint_plan.hints 表。

KingbaseES Hint 使用的更多相关文章

  1. KingbaseES V8R3集群运维案例之---主库系统down failover切换过程分析

    ​ 案例说明: KingbaseES V8R3集群failover时两个cluster都会触发,但只有一个cluster会调用脚本去执行真正的切换流程,另一个有对应的打印,但不会调用脚本,只是走相关的 ...

  2. KingbaseES 局部索引

    一个列要不要建立btree索引,判断条件是其键值分布是否够离散,比如主键.唯一键,可以建立索引.如果这个列有大量重复的值,则建立索引没有意义. 在生产环境中常会碰到键值分布不均匀的列,如表t1有一个名 ...

  3. KingbaseES V8R6集群管理运维案例之---repmgr standby switchover故障

    案例说明: 在KingbaseES V8R6集群备库执行"repmgr standby switchover"时,切换失败,并且在执行过程中,伴随着"repmr stan ...

  4. KingbaseES V8R3集群管理维护案例之---集群迁移单实例架构

    案例说明: 在生产中,需要将KingbaseES V8R3集群转换为单实例架构,可以采用以下方式快速完成集群架构的迁移. 适用版本: KingbaseES V8R3 当前数据库版本: TEST=# s ...

  5. KingbaseES V8R3集群管理和维护案例之---failover切换wal日志变化分析

    ​ 案例说明: 本案例通过对KingbaseES V8R3集群failover切换过程进行观察,分析了主备库切换后wal日志的变化,对应用者了解KingbaseES V8R3(R6) failover ...

  6. KingbaseES V8R3集群维护案例之---在线添加备库管理节点

    案例说明: 在KingbaseES V8R3主备流复制的集群中 ,一般有两个节点是集群的管理节点,分为master和standby:如对于一主二备的架构,其中有两个节点是管理节点,三个数据节点:管理节 ...

  7. KingbaseES应对表年龄增长过快导致事务回卷

    背景 前几天碰到这样一个场景,在KingbaseES数据库当作数据同步节点.其特点是接收过来的数据量巨大,其更新超级频繁,最大的数据库达到6TB.这还不是主要的,主要导致问题发生原因是同步数据库有很多 ...

  8. Hint 使用--leading

    Oracle hint -- leading 的作用是提示优化器某张表先访问,可以指定一张或多张表,当指定多张表时,表示按指定的顺序访问这几张表.而 Postgresql leading hint的功 ...

  9. KingbaseES V8R6单实例外部备份故障案例

    案例说明: 在KingbaseES V8R6单实例环境,配置外部备份服务器使用sys_backup.sh物理备份时,出现以下"WAL segment xxx was not archived ...

随机推荐

  1. SAP Word97 Intergration

    *&---------------------------------------------------------------------* *& Report SAPRDEMOW ...

  2. RPA应用场景-公积金贷款业务数据整合和报送

    场景概述 公积金贷款业务数据整合和报送 所涉系统名称 个贷系统.公积金管理系统 人工操作(时间/次) 0.5小时 所涉人工数量1000操作频率 每日 场景流程 1.机器人整理个人贷款信息.个人贷款账户 ...

  3. bat-命令行配置静态IP地址

    查看连接名称ipconfig 打开命令提示符,输入netsh后回车 输入interface后回车 输入ip,回车 输入set address "连接名称" static 新IP地址 ...

  4. idea部署项目运行没问题,但是页面404。

    解决方案: 这个位置不要添加内容. 参考:https://blog.csdn.net/hupixiong/article/details/105443606

  5. Linux YUM yum 命令详解

    Yum命令 常用yum命令列表 command is one of: * install package1 [package2] [...] * update [package1] [package2 ...

  6. 最优化:凸集、凸函数、KKT条件极其解释

    1.凸集(大概定义) 2.凸函数  3.KK条件      

  7. Effective Java 3 读后感

    Effective Java 3 读后感 最近学习了一下Effectvie Java,这是一本非常适合有一定经验的Java后端人员阅读的书.书中总结许多编码经验对开发很有帮助,比如其中总结的对于流和L ...

  8. SQLZOO练习四--SUM and COUNT(聚合函数)

    World Country Profile: Aggregate functions This tutorial is about aggregate functions such as COUNT, ...

  9. CA证书、自颁发证书、自签名证书联系

    一.理论基础 ssl:secure socket layer(安全套接层协议)的缩写,通过此协议可以保证两个应用通信的可靠性和保密性.openssl:是ssl协议的实现.提供了对称加密算法.非对称加密 ...

  10. 华为云Stack南向开放框架,帮助生态伙伴高效入云

    摘要:CloudBonder的生态社区通过一系列生态项目,解决提交叉组合.架构分层不清晰.运维界面不清晰等问题,简化对接流程,降低生态伙伴对接成本,缩短对接时间. 本文分享自华为云社区<[华为云 ...