openGauss/MogDB 5.0.0支持SQL PATCH

2023年 9月 28日 19.3k 0

原作者:何放

SQL PATCH简介

SQL PATCH是openGauss/MogDB 5.0.0的新特性,如果用户业务SQL由于数据等各种因素造成执行计划不优,导致性能变差,SQL未得达到预期的执行效果,就可以使用到SQL PATCH的机质Hint对查询计划进行调优或者对该语句进行报错短路处理。

SQL PATCH描述

SQL PATCH是可以提供给DBA、运维人员及其他需要对SQL进行调优的角色使用的,用户通过其他运维视图或定位手段识别到业务语句存在计划不优导致的性能问题时,可以通过创建SQL PATCH对业务语句进行基于Hint的调优。此外,对于部分由特定语句触发系统内部问题导致系统可服务性受损的语句,在不对业务语句变更的情况下,也可以通过创建用于单点规避的SQL PATCH,对问题场景提前报错处理,避免更大的损失。SQL PATCH可以做到两点,一是对SQL的调优,二是对SQL的规避,SQL PATCH的DBE_SQL_UTIL工具包函数的创建依赖Unique SQL ID,所以需要打开track_stmt_stat_level参数的FULL SQL,从而可以在statement_history中找到query的Unique SQL ID。

SQL PATCH工具包及系统表

SQL PATCH工具包为DBE_SQL_UTIL,包含函数6个create_hint_sql_patch,create_abort_sql_patch,drop_sql_patch,enable_sql_patch,disable_sql_patch,show_sql_patch。

GS_SQL_PATCH系统表存储所有SQL_PATCH的状态信息。

名称 类型 描述
patch_name name PATCH名称。
unique_sql_id bigint 查询全局唯一ID。
owner oid PATCH的创建用户ID。
enable bool PATCH是否生效。
status “char” PATCH的状态(预留字段)。
abort bool 是否是AbortHint。
hint_string text Hint文本。
hint_node pg_node_tree Hint解析&序列化的结果。
original_query text 原始语句(预留字段)。
patched_query text PATCH之后的语句(预留字段)。
original_query_tree pg_node_tree 原始语句的解析结果(预留字段)。
patched_query_tree pg_node_tree PATCH之后语句的解析结果(预留字段)。
Description text PATCH的备注。

SQL PATCH使用场景

场景1,SQL补丁工具对语句进行hint优化。

openGauss=# set track_stmt_stat_level = 'L1,L1'; --打开FullSQL统计信息
SET
openGauss=# select * from hint_t1 t1 where t1.a = 1; --执行SQL语句
a | b | c
---+---+---
1 | 1 | 1
(1 row)
openGauss=# select unique_query_id, query, query_plan from dbe_perf.statement_history where query like '%hint_t1%'; --获取查询计划和Unique SQL ID
-[ RECORD 1 ]---+----------------------------------------------------------------------------------------------
unique_query_id | 2578396627
query | select * from hint_t1 t1 where t1.a = ?;
query_plan | Datanode Name: sgnode
| Bitmap Heap Scan on hint_t1 t1 (cost=4.33..15.70 rows=10 p-time=0 p-rows=0 width=12)
| Recheck Cond: (a = '***')
| -> Bitmap Index Scan on hint_t1_a_idx (cost=0.00..4.33 rows=10 p-time=0 p-rows=0 width=0)
| Index Cond: (a = '***')
|
|
openGauss=# select * from dbe_sql_util.create_hint_sql_patch('patch1', 2578396627, 'indexscan(t1)'); -- 对指定的Unique SQL ID指定Hint Patch
-[ RECORD 1 ]---------+--
create_hint_sql_patch | t
openGauss=# explain select * from hint_t1 t1 where t1.a = 1; -- 通过explain可以确认Hint是否生效
NOTICE: Plan influenced by SQL hint patch
QUERY PLAN
-----------------------------------------------------------------------------------
[Bypass]
Index Scan using hint_t1_a_idx on hint_t1 t1 (cost=0.00..32.43 rows=10 width=12)
Index Cond: (a = 1)
(3 rows)
openGauss=# select * from hint_t1 t1 where t1.a = 1; -- 再次执行语句
a | b | c
---+---+---
1 | 1 | 1
(1 row)
openGauss=# select unique_query_id, query, query_plan from dbe_perf.statement_history where query like '%hint_t1%'; -- 可以看到新的执行记录计划已改变
-[ RECORD 1 ]---+--------------------------------------------------------------------------------------------------
unique_query_id | 2578396627
query | select * from hint_t1 t1 where t1.a = ?;
query_plan | Datanode Name: sgnode
| Bitmap Heap Scan on hint_t1 t1 (cost=4.33..15.70 rows=10 p-time=0 p-rows=0 width=12)
| Recheck Cond: (a = '***')
| -> Bitmap Index Scan on hint_t1_a_idx (cost=0.00..4.33 rows=10 p-time=0 p-rows=0 width=0)
| Index Cond: (a = '***')
|
|
-[ RECORD 2 ]---+--------------------------------------------------------------------------------------------------
unique_query_id | 2578396627
query | select * from hint_t1 t1 where t1.a = ?;
query_plan | Datanode Name: sgnode
| Index Scan using hint_t1_a_idx on hint_t1 t1 (cost=0.00..8.27 rows=1 p-time=0 p-rows=0 width=12)
| Index Cond: (a = '***')
|
|

场景2,SQL补丁工具对特定语句进行提前报错规避。

openGauss=# select * from dbe_sql_util.drop_sql_patch('patch1'); -- 删去patch1
drop_sql_patch
----------------
t
(1 row)
openGauss=# select * from dbe_sql_util.create_abort_sql_patch('patch2', 2578396627); 对该语句的Unique SQL ID创建Abort Patch
create_abort_sql_patch
------------------------
t
(1 row)

openGauss=# select * from hint_t1 t1 where t1.a = 1; -- 再次执行语句会提前报错
ERROR: Statement 2578396627 canceled by abort patch patch2

以上举例参考的官方文档

参考文档

https://docs.opengauss.org/zh/docs/5.0.0/docs/AboutopenGauss/支持SQL-PATCH.html

原作者:何放

SQL PATCH简介

SQL PATCH是openGauss/MogDB 5.0.0的新特性,如果用户业务SQL由于数据等各种因素造成执行计划不优,导致性能变差,SQL未得达到预期的执行效果,就可以使用到SQL PATCH的机质Hint对查询计划进行调优或者对该语句进行报错短路处理。

SQL PATCH描述

SQL PATCH是可以提供给DBA、运维人员及其他需要对SQL进行调优的角色使用的,用户通过其他运维视图或定位手段识别到业务语句存在计划不优导致的性能问题时,可以通过创建SQL PATCH对业务语句进行基于Hint的调优。此外,对于部分由特定语句触发系统内部问题导致系统可服务性受损的语句,在不对业务语句变更的情况下,也可以通过创建用于单点规避的SQL PATCH,对问题场景提前报错处理,避免更大的损失。SQL PATCH可以做到两点,一是对SQL的调优,二是对SQL的规避,SQL PATCH的DBE_SQL_UTIL工具包函数的创建依赖Unique SQL ID,所以需要打开track_stmt_stat_level参数的FULL SQL,从而可以在statement_history中找到query的Unique SQL ID。

SQL PATCH工具包及系统表

SQL PATCH工具包为DBE_SQL_UTIL,包含函数6个create_hint_sql_patch,create_abort_sql_patch,drop_sql_patch,enable_sql_patch,disable_sql_patch,show_sql_patch。

GS_SQL_PATCH系统表存储所有SQL_PATCH的状态信息。

名称 类型 描述
patch_name name PATCH名称。
unique_sql_id bigint 查询全局唯一ID。
owner oid PATCH的创建用户ID。
enable bool PATCH是否生效。
status “char” PATCH的状态(预留字段)。
abort bool 是否是AbortHint。
hint_string text Hint文本。
hint_node pg_node_tree Hint解析&序列化的结果。
original_query text 原始语句(预留字段)。
patched_query text PATCH之后的语句(预留字段)。
original_query_tree pg_node_tree 原始语句的解析结果(预留字段)。
patched_query_tree pg_node_tree PATCH之后语句的解析结果(预留字段)。
Description text PATCH的备注。

SQL PATCH使用场景

场景1,SQL补丁工具对语句进行hint优化。

openGauss=# set track_stmt_stat_level = 'L1,L1'; --打开FullSQL统计信息
SET
openGauss=# select * from hint_t1 t1 where t1.a = 1; --执行SQL语句
a | b | c
---+---+---
1 | 1 | 1
(1 row)
openGauss=# select unique_query_id, query, query_plan from dbe_perf.statement_history where query like '%hint_t1%'; --获取查询计划和Unique SQL ID
-[ RECORD 1 ]---+----------------------------------------------------------------------------------------------
unique_query_id | 2578396627
query | select * from hint_t1 t1 where t1.a = ?;
query_plan | Datanode Name: sgnode
| Bitmap Heap Scan on hint_t1 t1 (cost=4.33..15.70 rows=10 p-time=0 p-rows=0 width=12)
| Recheck Cond: (a = '***')
| -> Bitmap Index Scan on hint_t1_a_idx (cost=0.00..4.33 rows=10 p-time=0 p-rows=0 width=0)
| Index Cond: (a = '***')
|
|
openGauss=# select * from dbe_sql_util.create_hint_sql_patch('patch1', 2578396627, 'indexscan(t1)'); -- 对指定的Unique SQL ID指定Hint Patch
-[ RECORD 1 ]---------+--
create_hint_sql_patch | t
openGauss=# explain select * from hint_t1 t1 where t1.a = 1; -- 通过explain可以确认Hint是否生效
NOTICE: Plan influenced by SQL hint patch
QUERY PLAN
-----------------------------------------------------------------------------------
[Bypass]
Index Scan using hint_t1_a_idx on hint_t1 t1 (cost=0.00..32.43 rows=10 width=12)
Index Cond: (a = 1)
(3 rows)
openGauss=# select * from hint_t1 t1 where t1.a = 1; -- 再次执行语句
a | b | c
---+---+---
1 | 1 | 1
(1 row)
openGauss=# select unique_query_id, query, query_plan from dbe_perf.statement_history where query like '%hint_t1%'; -- 可以看到新的执行记录计划已改变
-[ RECORD 1 ]---+--------------------------------------------------------------------------------------------------
unique_query_id | 2578396627
query | select * from hint_t1 t1 where t1.a = ?;
query_plan | Datanode Name: sgnode
| Bitmap Heap Scan on hint_t1 t1 (cost=4.33..15.70 rows=10 p-time=0 p-rows=0 width=12)
| Recheck Cond: (a = '***')
| -> Bitmap Index Scan on hint_t1_a_idx (cost=0.00..4.33 rows=10 p-time=0 p-rows=0 width=0)
| Index Cond: (a = '***')
|
|
-[ RECORD 2 ]---+--------------------------------------------------------------------------------------------------
unique_query_id | 2578396627
query | select * from hint_t1 t1 where t1.a = ?;
query_plan | Datanode Name: sgnode
| Index Scan using hint_t1_a_idx on hint_t1 t1 (cost=0.00..8.27 rows=1 p-time=0 p-rows=0 width=12)
| Index Cond: (a = '***')
|
|

场景2,SQL补丁工具对特定语句进行提前报错规避。

openGauss=# select * from dbe_sql_util.drop_sql_patch('patch1'); -- 删去patch1
drop_sql_patch
----------------
t
(1 row)
openGauss=# select * from dbe_sql_util.create_abort_sql_patch('patch2', 2578396627); 对该语句的Unique SQL ID创建Abort Patch
create_abort_sql_patch
------------------------
t
(1 row)

openGauss=# select * from hint_t1 t1 where t1.a = 1; -- 再次执行语句会提前报错
ERROR: Statement 2578396627 canceled by abort patch patch2

以上举例参考的官方文档

参考文档

https://docs.opengauss.org/zh/docs/5.0.0/docs/AboutopenGauss/支持SQL-PATCH.html

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论