在KingbaseES中使用HINT
关键字:
执行计划、EXPLAIN、HINT、人大金仓、KingbaseES
一、摘要
本文介绍了KINGBASE中添加HINT的方法,以及各类HINT的使用方法。
什么是HINT
在KingbaseES中,HINT是一种用于指导查询优化器查询的指令或提示,以注释的形式添加到SQL查询语句中,目的是影响查询执行计划的生成和选择。
KingbaseES中使用HINT
HINT通过在目标SQL语句SELECT之后给出的特殊形式的注释来读取HINT注释。注释的形式以字符序列‘/*+’开头以‘*/’结尾,例如/*+SeqScan(tablename)*/,其具体使用方法如下:
- 在kingbase.conf配置文件中,配置:
enable_hint = on
- 启动数据库。
- 使用Hint的注释使Hint生效。
对于一条SQL语句“SELECT * FROM lineitem, orders WHERE l_orderkey = o_orderkey;”,在未使用HINT时查询,查询计划如下:
EXPLAIN SELECT * FROM lineitem, orders WHERE l_orderkey = o_orderkey;
QUERY PLAN
--------------------------------------------------------------------------------
Hash Join (cost=614274.00..2619482.95 rows=59986052 width=240)
Hash Cond: (lineitem.l_orderkey = orders.o_orderkey)
-> Seq Scan on lineitem (cost=0.00..1847745.52 rows=59986052 width=129)
-> Hash (cost=426774.00..426774.00 rows=15000000 width=111)
-> Seq Scan on orders (cost=0.00..426774.00 rows=15000000 width=111)
(5 rows)
通过添加HINT,查询计划返回的结果如下:
EXPLAIN SELECT /*+nestloop(lineitem orders)*/ * FROM lineitem, orders
QUERY PLAN
---------------------------------------------------------------------------------------------------------
Gather (cost=1000.57..15098325.58 rows=59986052 width=240)
Workers Planned: 32
-> Nested Loop (cost=0.56..9098720.38 rows=1874564 width=240)
-> Parallel Seq Scan on orders (cost=0.00..281461.50 rows=468750 width=111)
-> Index Scan using lineitem_l_orderkey_idx on lineitem (cost=0.56..17.20 rows=161 width=129)
Index Cond: (l_orderkey = orders.o_orderkey)
(6 rows)
可以看到通过添加HINT,表lineitem和表orders之间的连接,有原始计划中Hash Join,改变为Nest Loop Join。通过添加HINT:“nestloop”改变表与表之间的连接方式。
KingbaseES中HINT类型
版本
类型
HINT
V8R6C4
表扫描
SeqScan(table)
TidScan(table)
IndexScan(table[ index...])
IndexOnlyScan(table[ index...])
BitmapScan(table[ index...])
NoSeqScan(table)
NoTidScan(table)
NoIndexScan(table)
NoIndexOnlyScan(table)
NoBitmapScan(table)
IndexScanRegexp (table[regexp...])
BitmapScanRegexp (table[regexp...])
IndexOnlyScanRegexp(table[regexp...])
ForceSeqScan(table)
ForceTidScan(table)
ForceIndexScan(table[ index...])
ForceIndexOnlyScan(table[ index...])
ForceBitmapScan(table[ index...])
连接方式
NestLoop(table table[ table...])
HashJoin(table table[ table...])
MergeJoin(table table[ table...])
NoNestLoop(table table[ table...])
NoHashJoin(table table[ table...])
NoMergeJoin(table table[ table...])
ForceNestLoop(table table[ table...])
ForceHashJoin(table table[ table...])
ForceMergeJoin(table table[ table...])
连接顺序
leading(join_table_list)
leading((outer_table inner_table))
行数更正
rows(table_list #|+|-|* const)
并行执行
Parallel(table_name workers)
设置GUC参数
Set(Param_Name Param_Value)
V8R6C5
块命名
blockname(subquery_name)
Nestloop内表物化
materialize(inner_table_list)
Nestloop内表使用索引
use_nl_with_index(inner_table)
表连接顺序
ordered
聚集
Hashagg
Groupagg