.. _使用HINT:

=============================
使用SYS_HINT_PLAN（停止维护）
=============================

本章节包含以下内容：

- 概述_
- SYS_HINT_PLAN的功能_
- SYS_HINT_PLAN的使用_
- 配置参数_
- 示例_


概述
---------

查询优化器在尽量提供最优的执行计划，但是因为种种原因，优化器以及它所依赖的统计信息存在着自由的局限性。所以，很多情况下，优化器提供的统计信息不是最优，需要人工干预。

SYS_HINT_PLAN则是为了解决该问题而提供的一项功能，可以帮助用户人为的控制执行计划。


.. note::

   该功能在KingbaseES
   V8R6C3的版本后已不再更新，故不再继续推荐使用，我们在KingbaseES
   V8R6C4版本开始增强了该功能，详细说明请参照本手册章节 使用HINT



SYS_HINT_PLAN的功能
-------------------

根据HINT注释作用的对象的种类以及如何来影响计划，HINT注释可以分为六类，
Scaning methods, join methods, joining order, row number correction,
parallel query和GUC setting。

1. **Scan method**

  Scan method相关的HINT注释只会在普通表、继承表、UNLOGGED表、临时表和系统目录上生效，其括号内的值只能是一个关系。另外，也可以在一个查询中，对其中的多个目标表进行干预。它包括

  -  SeqScan(table)：强制在表上使用顺序扫描。

  -  TidScan(table)：强制在表上使用Tid扫描。

  -  IndexScan(table[index...])：强制在表上使用索引扫描，只限制指定的索引。

  -  IndexOnlyScan(table[ index...])：强制在表上使用Indexonly scan，限制特定的索引。当Indexonlyscan不可用时，可以使用索引扫描。

  -  BitmapScan(table[ index...])：强制在表上使用位图扫描。

  -  NoSeqScan(table)：强制不在表上使用顺序扫描。

  -  NoTidScan(table)：强制不在表上使用Tid扫描。

  -  NoIndexScan(table)：强制不在表上使用索引扫描和indexonlyscan。

  -  NoIndexOnlyScan(table)：强制不在表上使用indexonlyscan。

  -  NoBitmapScan(table)：强制不在表上使用位图索引。

2. **Join method**

  Join method相关HINT注释可以影响普通表、继承表、UNLOGGED表、临时表、外部表、系统目录、表函数、VALUES命令结果和CTE上的连接。HINT注释括号内的值的顺序对执行计划的执行顺序没有影响。它包括：

  -  NestLoop(table table[table...])：在对指定的表进行连接时，强制使用循环嵌套连接。

  -  HashJoin(table table[table...])：在对指定的表进行连接时，强制使用散列连接。

  -  MergeJoin(table table[table...])：在对指定的表进行连接时，强制使用排序合并连接。

  -  NoNestLoop(table table[table...])：在对指定的表进行连接时，强制不使用循环嵌套连接。

  -  NoHashJoin(table table[table...])：在对指定的表进行连接时，强制不使用散列连接。

  -  NoMergeJoin(table table[table...])：在对指定的表进行连接时，强制不使用排序合并连接。

3. **Join order**

  Leading短语括号中的值必须为两个或多个关系，并且每个关系不能重复。在使用Leading时，可以使用圆括号嵌套来干预连接操作的顺序。使用UNION操作符对多个SELECT语句进行合并时，
  UNION内部的各个查询语句之间的连接顺序无法用Leading短语干预。主要有以下两种形式：

  -  Leading(table table[ table...])：强制使用指定的连接顺序。

  -  Leading(<join pair>)：强制使用指定的连接顺序和方向。其中一个joinpair可以是一对表和/或者用括号括起来的可以构成嵌套结构的其他连接对。

4) **Row number correction**

  Rows短语的圆括号内必须包含两个或者两个以上的关系。当圆括号内存在多个不相关连接时，只有最左边的连接生效。当查询中有where子句时，依然不影响对结果的修正。

  -  Rows(table table[ table...]correction)：修正特定的表连接后的结果的行数，

  可用的修正方法有absolute(#<n>)，addition (+<n>)，subtract (-<n>) 和multiplication (*<n>)，<n>必须是函数strtod()可以读取的字符串。

5) **Parallel query configuration**

  它对普通表，继承父项，UNLOGGED表和系统目录产生影响。外部表，表函数，values子句，CTE，视图和子查询不受影响。

  -  Parallel(table <# of workers>[soft|hard])：在指定的表上强制执行或者禁止并行执行。

  <# of workers>是所需的并行workers数， 0代表禁止并行执行。
  如果第三个参数是soft (默认值)，仅会改变max_parallel_workers_per_gather 并把所有事情留给优化器。
  Hard表示把workers数强制为指定的值。

6) **GUC setting**

  Set短语可以一次对多个参数进行修改，但是当多个相同的hint修改同一个GUC参数时，只有最后运行的生效。参数的修改只在此次的查询有效。

  -  Set(GUC-param value)：在优化器运行时，将这个GUC参数设置为指定的值。

sys_hint_plan也可以用于在PLSQL脚本中的查询，但有一定的限制。仅适用于返回一行的查询(SELECT,
INSERT, UPDATE, DELETE)、返回多行的查询(RETURN
QUERY)、动态SQL语句(EXECTE)、Cursor开启(OPEN)、循环查询的结果(FOR)。在PLSQL中HINT注释需要放在SELECT与目标列之间。

SYS_HINT_PLAN的使用
-------------------

HINT是以插件形式提供给用户的。所以，在使用前，需要通过SQL语句“CREATE
EXTENSION sys_hint_plan”来创建hint拓展。然后使用LOAD
‘sys_hint_plan’命令来加载并激活它来使用，但这种方式只能使这个插件在本次会话生效。

另外，也可以使用SQL语句alter user kingbase set session_preload_libraries
= 'sys_hint_plan';和alter database test set
session_preload_libraries='sys_hint_plan'来实现用户和数据库级自动加载;
通过将参数shared_preload_libraries的值修改为‘sys_hint_plan’，
然后重启数据库服务来使sys_hint_plan生效，这种方式来实现cluster级的加载。

sys_hint_plan通过在目标SQL语句给出的特殊形式的注释来读取HINT注释。注释的形式以字符序列‘/*+’开头以‘*/’结尾，例如/*+SeqScan*(tablename)*/。另外，hints短语会区别其中对象名称的大小写，即大小写敏感。

*注：在使用形式上，除了在SQL语句前显示的指定HINT注释，还可以通过HINT_TABLE来指对SQL语句做映射，也就是说在匹配到特定的语句时，自动加载预先设定的规则，可以无需修改应用。具体使用见11.5示例。*

配置参数
--------

.. table::
   :widths: 3 2 1  
   :class: longtable

   +----------------------------+----------------------------+---------+
   | Parameter name             | discription                | Default |
   +============================+============================+=========+
   | sys_hint_plan.enable_hint  | 启用sys_hint_plan。        | on      |
   +----------------------------+----------------------------+---------+
   | sys_h                      | True enbles hinting by     | off     |
   | int_plan.enable_hint_table | table。                    |         |
   +----------------------------+----------------------------+---------+
   | sy                         | 指定hints解析错误          | INFO    |
   | s_hint_plan.parse_messages | 的日志级别。可用的值error, |         |
   |                            | warning, notice, info,     |         |
   |                            | log, debug。               |         |
   +----------------------------+----------------------------+---------+
   | sys_hint_plan.debug_print  | 控制debug信息              | off     |
   |                            | 打印和详细程度。可用的值为 |         |
   |                            | off, on, detailed and      |         |
   |                            | verbose。                  |         |
   +----------------------------+----------------------------+---------+
   | s                          | 指定debug打印的            | INFO    |
   | ys_hint_plan.message_level | 信息级别。可用的值为error, |         |
   |                            | warning, notice, info,     |         |
   |                            | log, debug。               |         |
   +----------------------------+----------------------------+---------+

参数sys_hint_plan.enable_hint默认开启，当此参数关闭后，SQL查询语句中的HINT注释将不会对执行计划产生影响。

当参数sys_hint_plan.debug_print设置为on，并且sys_hint_plan.message_level设置为log后，在一个带有格式正确的HINT注释的SQL语句运行时，会一同输出hint的调试信息，内容包含usedhint、not
used hint、duplication hint、error hint。

当运行create extension sys_hint_plan时，会伴随着创建一个名为hint_plan的模式，并在hint_plan模式下创建一个名为hints的表，此表归创建者所有，其他用户默认情况下只有查询权限。表的模式如下：

.. table:: 
   :class: longtable

   +-------------------+-------------------------------------------------+
   | column            | description                                     |
   +===================+=================================================+
   | id                | 用于标识每行的唯一编号。此列按顺序自动填充。    |
   +-------------------+-------------------------------------------------+
   | norm_query_string | HINT注释影响的                                  |
   |                   | 目标sql查询语句。查询语句中的常量必须替换为’?’  |
   +-------------------+-------------------------------------------------+
   | application_name  | 要应用hint的会话的application_name值            |
   +-------------------+-------------------------------------------------+
   | hints             | 使用的HINT注释。其中不包括注释标记’/*+’和’*/’   |
   +-------------------+-------------------------------------------------+

这个表的是否启用与参数sys_hint_plan.enable_hint_table有关。不管这个参数是off还是on，表的拥有者都可以向其中插入数据。当GUC参数sys_hint_plan.enable_hint_plan的值设置为on时，表中的数据才能生效。当表中数据生效时，列norm_query_string下的查询语句会被同一行的hints列下的hints短语所干预。

示例
----

**1）强制在扫描时使用Seqscan**

  数据准备：

  .. code::

   
      CREATE TABLE t1 (id int PRIMARY KEY, val int);
      CREATE TABLE t2 (id int PRIMARY KEY, val int);
      INSERT INTO t1 SELECT i, i % 100 FROM (SELECT generate_series(1, 10000) i) t;
      INSERT INTO t2 SELECT i, i % 10 FROM (SELECT generate_series(1, 1000) i) t;
      analyze;
      load 'sys_hint_plan';
      create extension sys_hint_plan;
      set sys_hint_plan.enable_hint to on;


  \使用“/*+SeqScan(T1) SeqScan(T2)*/”让表t1、t2扫描时使用顺序扫描


  .. code::

      /*+SeqScan(T1) SeqScan(T2)*/
      EXPLAIN analyse SELECT * FROM t1, t2 WHERE t1.id = t2.id;
      
      QUERY PLAN
      --------------------------------------------------------------------
      Hash Join (cost=27.50..220.00 rows=1000 width=16) (actual time=1.354..6.377 rows=1000 loops=1)
          Hash Cond: (t1.id = t2.id)
          -> Seq Scan on t1 (cost=0.00..145.00 rows=10000 width=8) (actual time=0.030..2.046 rows=10000 loops=1)
          -> Hash (cost=15.00..15.00 rows=1000 width=8) (actual time=1.299..1.299 rows=1000 loops=1)
              Buckets: 1024 Batches: 1 Memory Usage: 48kB 
              Seq Scan on t2 (cost=0.00..15.00 rows=1000 width=8) (actual time=0.016..0.579 rows=1000 loops=1)
      Planning time: 0.635 ms
      Execution time: 6.606 ms
      (8 行记录)


\
**2）强制在连接时使用MergeJoin**

  数据准备：

  .. code::

      CREATE TABLE t3 (id int PRIMARY KEY, val int);
      CREATE TABLE t4 (id int PRIMARY KEY, val int);
      INSERT INTO t3 SELECT i, i FROM (SELECT generate_series(1, 100) i) t;
      INSERT INTO t4 SELECT i, i FROM (SELECT generate_series(1, 10) i) t;
      analyze;
      load 'sys_hint_plan'
      set sys_hint_plan.enable_hint to on; 



  \使用“/*+MergeJoin(T4 T1 T2 T3)*/”让表t1,t2,t3,t4连接时使用mergejoin。

  .. code::

      /*+MergeJoin(T4 T1 T2 T3)*/
      EXPLAIN analyse SELECT * FROM t1, t2, t3, t4 WHERE t1.id = t2.id AND t1.id = t3.id AND t1.id = t4.id;
      
      QUERY PLAN
      --------------------------------------------------------------------
      Merge Join (cost=1.97..4.34 rows=1 width=32) (actual time=0.178..0.313 rows=10 loops=1)
          Merge Cond: (t1.id = t3.id)
          ->Merge Join (cost=0.56..90.36 rows=1000 width=16) (actual time=0.098..0.130 rows=11 loops=1)
              Merge Cond: (t1.id = t2.id)
              -> Index Scan using t1_pkey on t1 (cost=0.29..318.29 rows=10000 width=8) (actual time=0.058..0.069 rows=11 loops=1)
              -> Index Scan using t2_pkey on t2 (cost=0.28..43.27 rows=1000 width=8) (actual time=0.027..0.036 rows=11 loops=1)
          -> Materialize (cost=1.41..2.82 rows=10 width=16) (actual time=0.074..0.110 rows=10 loops=1)
              -> Merge Join (cost=1.41..2.80 rows=10 width=16) (actual time=0.066..0.095 rows=10 loops=1)
                  Merge Cond: (t3.id = t4.id)
                  -> Index Scan using t3_pkey on t3 (cost=0.14..13.64 rows=100 width=8) (actual time=0.010..0.022 rows=11 loops=1)
                  -> Sort (cost=1.27..1.29 rows=10 width=8) (actual time=0.050..0.056 rows=10 loops=1)
                      Sort Key: t4.id
                      Sort Method: quicksort Memory: 25kB
                      -> Seq Scan on t4 (cost=0.00..1.10 rows=10 width=8) (actual time=0.018..0.024 rows=10 loops=1)
      Planning time: 2.077 ms
      Execution time: 0.546 ms
      (16 行记录)


\
**3）强制指定多表连接时的连接顺序**

  使用/*+Leading(((T1 T2) T3))*/指定表t1、t2、t3的连接顺序


  .. code::

      /*+Leading(((T1 T2) T3))*/
      EXPLAIN (COSTS false) SELECT * FROM t1, t2, t3 WHERE t1.id = t2.id AND t2.val = t3.val AND t1.id < 10;
      
      QUERY PLAN
      --------------------------------------------------------------------
      Nested Loop
          Join Filter: (t2.val = t3.val)
          -> Hash Join
              Hash Cond: (t1.id = t2.id)
              -> Index Scan using t1_pkey on t1
                  Index Cond: (id < 10)
              -> Hash
                  -> Seq Scan on t2
          -> Seq Scan on t3
      (9 行记录)


\
**4）修正连接操作的行数**

  .. code::

      /*+ Rows(A B #10) */ SELECT... ; 把连接后的行数设定为10
      /*+ Rows(A B +10) */ SELECT... ; 从行数中增加10
      /*+ Rows(A B -10) */ SELECT... ; 从行数中减去10
      /*+ Rows(A B *10) */ SELECT... ; 将行数放大10倍
      /*+ Rows(T1 T3 T2 #22) */
      EXPLAIN SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id) JOIN t3 ON (t3.id = t2.id);
      
      QUERY PLAN
      --------------------------------------------------------------------
      Merge Join (cost=5.88..7.40 rows=22 width=24)
          Merge Cond: (t1.id = t3.id)
          -> Merge Join (cost=0.56..90.36 rows=1000 width=16)
              Merge Cond: (t1.id = t2.id)
              -> Index Scan using t1_pkey on t1 (cost=0.29..318.29 rows=10000 width=8)
              -> Index Scan using t2_pkey on t2 (cost=0.28..43.27 rows=1000 width=8)
          -> Sort (cost=5.32..5.57 rows=100 width=8)
              Sort Key: t3.id
              -> Seq Scan on t3 (cost=0.00..2.00 rows=100 width=8)
      (9 行记录)



\
**5) 扫描时强制执行并行执行配置**


  .. code::

      explain SELECT/*+ Parallel(t1 3 hard) Parallel(t2 5 hard) */ t2.id FROM t1 JOIN t2 ON (t1.val=t2.val);
      
      QUERY PLAN
      --------------------------------------------------------------------
      Hash Join (cost=125.00..1252.50 rows=100000 width=4)
          Hash Cond: (t2.val = t1.val)
          -> Gather (cost=0.00..0.00 rows=1000 width=8)
              Workers Planned: 5
              -> Parallel Seq Scan on t2 (cost=0.00..0.00 rows=417 width=8)
          -> Hash (cost=0.00..0.00 rows=10000 width=4)
              -> Gather (cost=0.00..0.00 rows=10000 width=4)
                  Workers Planned: 3
                  -> Parallel Seq Scan on t1 (cost=0.00..0.00 rows=3226 width=4)
      (9 rows)



\
**6) 优化器运行时修改相关的GUC参数**

  .. code::

      /*+ Set(random_page_cost 2.0) */
      SELECT * FROM t1 WHERE id = 20;


\
**7)在PLSQL中的使用**

  .. code::

      CREATE FUNCTION testfunc() RETURNS RECORD AS $$
      DECLARE
      ret record;
      BEGIN
      SELECT /*+ SeqScan(T1) */ * INTO ret FROM t1 LIMIT 1;
      RETURN ret;
      END;
      $$ LANGUAGE plsql;
      SELECT testfunc();
      LOG: sys_hint_plan:
      used hint:
      SeqScan(t1)
      not used hint:
      duplication hint:
      error hint:
      CONTEXT: SQL statement "SELECT /*+ SeqScan(t1) */ * FROM t1 LIMIT 1"
      PLSQL function testfunc() line 5 at SQL statement
      testfunc
      ----------
      (1,1)
      (1 row)


\
**8）参数sys_hint_plan.enable_hint_table的使用**

  .. code::

      set sys_hint_plan.enable_hint_table to on;
      show sys_hint_plan.enable_hint_table;
      sys_hint_plan.enable_hint_table
      --------------------------------
      on
      (1 行记录)
      
      
      explain analyze select * from t1 where t1.id = 10;
      
      QUERY PLAN
      ----------------------------------------------------------------------
      Index Scan using t1_pkey on t1 (cost=0.29..8.30 rows=1 width=8)(actual time=0.018..0.020 rows=1 loops=1)
          Index Cond: (id = 10)
      Planning time: 0.127 ms
      Execution time: 0.051 ms
      (4 行记录)
      
      
      INSERT INTO hint_plan.hints(norm_query_string, application_name, hints) VALUES ( 'Explain analyze select * from t1 where t1.id = ?;',' ','SeqScan(T1)');
      explain analyze select * from t1 where t1.id = 10;
      QUERY PLAN
      --------------------------------------------------------------------
      Seq Scan on t1 (cost=0.00..170.00 rows=1 width=8) (actual time=0.027..18.739 rows=1 loops=1)
          Filter: (id = 10)
          Rows Removed by Filter: 9999
      Planning time: 0.217 ms
      Execution time: 18.773 ms
      (5 行记录)）


