GaussDB 数据库架构师修炼(十八) SQL引擎-计划管理-SQL PATCH

1 业务背景

GaussDB的优化器生成计划不优的情况下,对DBA调优过程中不对业务sql修改场景下,提供3种计划管理,分别为plan hint,sql patch,spm。

2 sql patch的使用场景

在发现查询语句的执行计划 、执行方式未达预期的场景下,可以通过创建查询补丁的方式, 使用 Hint对查询计划进行调优或对特定的语句进行报错短路处理。

SQL Patch能够在避免直接修改用户业务语句的前提下对查询执行的方式做一定调整。

3 使用前提

1)、SQL Patch的实现基于Unique SQL ID,所以需要打开相关的运维参数才可以生效 ( enable_resource_track = on, instr_unique_sql_count > 0)。

2)、Unique SQL ID在WDR报告和慢SQL视图中都可以获取到,在创建SQL Patch时需要指定Unique SQL ID 。

3)、对于存储过程内的SQL则需要设置参数 instr_unique_sql_track_type = 'all' 后在 dbe_perf.statement_history视图中查询Unique SQL ID 。

4 使用举例

步骤1:创建测试数据

复制代码
[Ruby@dtest1 ~]$ gsql -h xx.xxx.xx.71 -dcsdn -p8000 -U csdn -W '******' -ar
gsql ((GaussDB Kernel 505.2.1 build 159cea95) compiled at 2024-12-27 09:22:44 commit 10161 last mr 21504 release)
SSL connection (cipher: ECDHE-RSA-AES128-GCM-SHA256, bits: 128)
Type "help" for help.

csdn=>
csdn=> set search_path=csdn;
SET
csdn=> CREATE TABLE hint_t1(a int, b int, c int);
CREATE TABLE
csdn=> CREATE  INDEX ON hint_t1(a);
CREATE INDEX
csdn=> INSERT  INTO hint_t1 VALUES(1, 1, 1);
INSERT 0 1

步骤2:打开FullSQL统计信息(track_stmt_stat_level:L1,L1)

复制代码
csdn=> SET track_stmt_stat_level  = 'L1,L1';
SET
csdn=> SET explain_perf_mode = normal;
SET

步骤3:登入postgres库获取unique sql id

复制代码
gsql -h ***.***.***71 -p 8000 -d postgres -U root -W ******* -ar
gsql ((GaussDB Kernel 505.2.1 build 159cea95) compiled at 2024-12-27 09:22:44 commit 10161 last mr 21504 release)
SSL connection (cipher: ECDHE-RSA-AES128-GCM-SHA256, bits: 128)
Type "help" for help.

gaussdb=> SELECT unique_query_id, query, query_plan  FROM dbe_perf.statement_history WHERE query  LIKE '%hint_t1%';
 unique_query_id |                   query                   |                                 query_plan

-----------------+-------------------------------------------+-------------------------------------------------------------------
----------
       868389431 | SELECT * FROM hint_t1 t1 WHERE t1.a =  ?; | Datanode Name: dn_6001_6002_6003
         +
                 |                                           | Bitmap Heap Scan on hint_t1 t1  (cost=4.33..14.88 rows=10 width=12
)        +
                 |                                           |   Recheck Cond: (a = '***')
         +
                 |                                           |   ->  Bitmap Index Scan on hint_t1_a_idx  (cost=0.00..4.33 rows=10
 width=0)+
                 |                                           |         Index Cond: (a = '***')
         +
                 |                                           |
         +
                 |                                           |
(1 row)

得到的unique sql id为:868389431

步骤4:登入业务库,对指定SQL使用SQL Patch

复制代码
SELECT * FROM dbe_sql_util.create_hint_sql_patch('patch1', 868389431, 'indexscan(t1)');
 create_hint_sql_patch
-----------------------
 t
(1 row)

csdn=> set search_path=csdn;
SET

步骤5:验证计划结果:

复制代码
explain SELECT *  FROM hint_t1 t1 WHERE t1.a = 1;
SET
NOTICE:  Plan influenced by SQL hint patch
 id |                    operation                     | E-rows | E-width |    E-costs
----+--------------------------------------------------+--------+---------+---------------
  1 | ->  Index Scan using hint_t1_a_idx on hint_t1 t1 |     10 |      12 | 0.000..32.425
(1 row)

   Predicate Information (identified by plan id)
----------------------------------------------------
   1 --Index Scan using hint_t1_a_idx on hint_t1 t1
         Index Cond: (a = 1)
(2 rows)

 ====== Query Others =====
---------------------------
 Bypass: Yes
(1 row)

执行计划结果:Plan influenct by SQL hint patch

步骤6:查看sql patch

复制代码
csdn=> select * from dbe_sql_util.show_sql_patch('patch1');
 unique_sql_id | enable | abort |   hint_str
---------------+--------+-------+---------------
     868389431 | t      | f     | indexscan(t1)
(1 row)

csdn=>

步骤7:禁用sql patch

复制代码
csdn=> select * from dbe_sql_util.disable_sql_patch('patch1');
 disable_sql_patch
-------------------
 t
(1 row)

csdn=>

步骤8:验证禁用之后sql patch

复制代码
csdn=> explain SELECT *  FROM hint_t1 t1 WHERE t1.a = 1;
 id |                  operation                   | E-rows | E-width |    E-costs
----+----------------------------------------------+--------+---------+---------------
  1 | ->  Bitmap Heap Scan on hint_t1 t1           |     10 |      12 | 4.328..14.883
  2 |    ->  Bitmap Index Scan using hint_t1_a_idx |     10 |       0 | 0.000..4.325
(2 rows)

 Predicate Information (identified by plan id)
-----------------------------------------------
   1 --Bitmap Heap Scan on hint_t1 t1
         Recheck Cond: (a = 1)
   2 --Bitmap Index Scan using hint_t1_a_idx
         Index Cond: (a = 1)
(4 rows)

csdn=>