当前位置: 首页 > news >正文

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=10width=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 patchid |                    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 t1Index 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.8832 |    ->  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 t1Recheck Cond: (a = 1)2 --Bitmap Index Scan using hint_t1_a_idxIndex Cond: (a = 1)
(4 rows)csdn=>

http://www.xdnf.cn/news/1352017.html

相关文章:

  • Base64编码、AES加密、RSA加密、MD5加密
  • RAG Embeddings 向量数据库
  • 使用Ollama部署自己的本地模型
  • 疯狂星期四文案网第48天运营日记
  • 12 SQL进阶-锁(8.20)
  • Python语法速成课程(二)
  • 科技赋能,宁夏农业绘就塞上新“丰”景
  • 进程的概念:进程调度算法
  • 【GPT入门】第57课 详解 LLamaFactory 与 XTuner 实现大模型多卡分布式训练的方案与实践
  • rust语言 (1.88) egui (0.32.1) 学习笔记(逐行注释)(七) 鼠标在控件上悬浮时的提示
  • linux中文本文件操作之grep命令
  • 【软件设计模式】策略模式
  • MySQL:事务管理
  • Intel RealSense D435 深度相机详解
  • Java 学习笔记(基础篇8)
  • Linux总线设备驱动模型深度理解
  • Vue3 学习教程,从入门到精通,基于 Vue 3 + Element Plus + ECharts + JavaScript的51购商城项目(45)
  • imx6ull-驱动开发篇37——Linux MISC 驱动实验
  • 大模型四种常见安全问题与攻击案例
  • MySQL数据库管理与索引优化全攻略
  • 力扣(全排列)
  • 使用 PSRP 通过 SSH 建立 WinRM 隧道
  • Linux-常用文件IO函数
  • jQuery 知识点复习总览
  • (nice!!!)(LeetCode 面试经典 150 题) 173. 二叉搜索树迭代器 (栈)
  • 55 C++ 现代C++编程艺术4-元编程
  • 数据结构与算法-字符串、数组和广义表(String Array List)
  • 【Tech Arch】Apache Flume海量日志采集的高速公路
  • 解码LLM量化:深入剖析最常见8位与4位核心算法
  • Mac相册重复照片终结指南:技术流清理方案