VastbaseG100

基于openGauss内核开发的企业级关系型数据库。

Menu

SQLdiag:慢SQL发现

功能描述

SQLdiag是一个SQL语句执行时间预测工具,通过模板化方法或者深度学习方法,实现在不获取SQL语句执行计划的前提下,依据语句逻辑相似度与历史执行记录,预测SQL语句的执行时间并以此发现异常SQL。

现有的预测技术主要基于执行计划的预测方法,但这些预测方案仅适用于OLAP场景且可以获取执行计划的任务,对于OLTP或者HTAP这样的快速、简单查询是没有太多使用价值的。与上述方案不同,SQLdiag着眼于数据库的历史SQL语句,通过对历史SQL语句的执行表现进行总结归纳,将之再用于推断新的未知业务上。

由于短时间内数据库SQL语句执行时长不会有太大的差距,SQLdiag可以从历史数据中检测出与已执行SQL语句相似的语句结果集,并基于SQL向量化技术通过SQL模板化和深度学习这两种方法来预测SQL语句执行时长。本工具有如下优点:

  • 不需要SQL语句的执行计划,对数据库性能不会有任何的影响。

  • 使用场景广泛,目前业内的很多算法局限性比较高,比如只适用于OLTP或者OLAP,而SQLdiag使用场景广泛。

  • 该工具容易理解,只需要简单的操作,就可以训练出自己的预测模型。

本工具的典型应用场景是对一批即将上线的SQL语句进行透视,提前识别风险。

语法格式

gs_dbmind component sqldiag --help 

显示如下帮助信息:

usage:   [-h] [-f CSV_FILE] [--predicted-file PREDICTED_FILE]
               [--model {template,dnn}] --model-path MODEL_PATH
               [--config-file CONFIG_FILE]
               {train,predict,finetune}

SQLdiag integrated by openGauss.

positional arguments:
  {train,predict,finetune}
                        The training mode is to perform feature extraction and
                        model training based on historical SQL statements. The
                        prediction mode is to predict the execution time of a
                        new SQL statement through the trained model.

optional arguments:
  -h, --help            show this help message and exit
  -f CSV_FILE, --csv-file CSV_FILE
                        The data set for training or prediction. The file
                        format is CSV. If it is two columns, the format is
                        (SQL statement, duration time). If it is three
                        columns, the format is (timestamp of SQL statement
                        execution time, SQL statement, duration time).
  --predicted-file PREDICTED_FILE
                        The file path to save the predicted result.
  --model {template,dnn}
                        Choose the model model to use.
  --model-path MODEL_PATH
                        The storage path of the model file, used to read or
                        save the model file.
  --config-file CONFIG_FILE

参数说明

参数 参数说明 取值范围
-f 训练或预测文件位置。 -
\--predicted-file 预测结果存储位置。 -
\--model 模型选择。 template, dnn
\--model-path 训练模型存储位置。 *

注意事项

  • 需要保证用户提供训练数据。
  • 如果用户通过提供的工具收集训练数据,则需要启用WDR功能,涉及到参数:track_stmt_stat_levellog_min_duration_statement
  • 为保证预测准确率,用户提供的历史语句日志应尽可能全面并具有代表性。

使用指导

SQL流水采集方法

本工具需要用户提前准备数据,训练数据格式如下,每个样本通过换行符分隔。

SQL,EXECUTION_TIME

预测数据格式如下:

SQL

其中SQL表示SQL语句的文本,EXECUTION_TIME表示SQL语句的执行时间。

用户可以按照要求格式自己收集训练数据,工具也提供了脚本自动采集(load_sql_from_rd),该脚本基于WDR报告获取SQL信息,涉及到的参数有”log_min_duration_statement”和”track_stmt_stat_level”。

  • 其中log_min_duration_statement表示慢SQL阈值,如果为0则全量收集,时间单位为毫秒。
  • track_stmt_stat_level表示信息捕获的级别,建议设置为track_stmt_stat_level='L0,L0'

参数开启后,可能占用一定的系统资源,但一般不大。持续的高并发场景可能产生5%以内的损耗,数据库并发较低的场景,性能损耗可忽略。

下述脚本存在于sqldiag根目录($GAUSSHOME/bin/components/sqldiag)中。

使用脚本获取训练集方式:

load_sql_from_wdr.py [-h] --port PORT --start_time START_TIME
                            --finish_time FINISH_TIME [--save_path SAVE_PATH]

例如:

python load_sql_from_wdr.py --start_time "2023-02-10 00:00:00" --finish_time "2023-02-15 00:00:00" --port 5432  --save_path ./data.csv

操作步骤

1、提供历史日志以供模型训练。

2、进行训练与预测操作。

基于模板法的训练与预测:

gs_dbmind component sqldiag [train, predict] -f FILE --model template --model-path template_model_path 

基于DNN的训练与预测:

gs_dbmind component sqldiag [train, predict] -f FILE --model dnn --model-path dnn_model_path

示例

下面示例使用安装后自带的训练文件进行演示,以下涉及到的sqldiag.conf配置文件路径和训练数据路径以实际为准。

1、修改GUC参数文件postgresql.conf。

log_min_duration_statement=30ms
track_stmt_stat_level='L0,L0'

2、重启数据库。

vb_ctl restart

3、切换到训练数据的目录下。

cd /home/vastbase_ai/local/vastbase_11395/bin/dbmind/components/sqldiag/sample_data

4、使用测试数据进行模板化训练。

gs_dbmind component sqldiag train -f ./train.csv --model template --model-path ./template --config-file /home/vastbase_ai/local/vastbase_11395/bin/dbmind/components/sqldiag/sqldiag.conf --predicted-file ./result/t_result

返回结果如下:

5、使用测试数据进行模板化预测。

gs_dbmind component sqldiag predict -f ./predict.csv --model template --model-path ./template --predicted-file ./result/t_result --config-file /home/vastbase_ai/local/vastbase/bin/dbmind/components/sqldiag/sqldiag.conf

6、使用测试数据进行模板化模型更新。

gs_dbmind component sqldiag finetune -f ./train.csv --model template --model-path ./template --config-file /home/vastbase_ai/local/vastbase_11395/bin/dbmind/components/sqldiag/sqldiag.conf --predicted-file ./result/t_result 

返回结果如下,模板化模型正常更新:

7、使用测试数据进行DNN训练。

gs_dbmind component sqldiag train -f ./train.csv --model dnn --model-path ./dnn_model --config-file /home/vastbase_ai/local/vastbase_11395/bin/dbmind/components/sqldiag/sqldiag.conf --predicted-file ./result/t_result 

8、使用测试数据进行DNN预测。

gs_dbmind component sqldiag predict -f ./predict.csv --model dnn --model-path ./dnn_model --predicted-file ./result/t_result1 --config-file /home/vastbase_ai/local/vastbase_11395/bin/dbmind/components/sqldiag/sqldiag.conf

返回结果如下:

9、使用测试数据进行DNN模型更新。

gs_dbmind component sqldiag finetune -f ./train.csv --model dnn --model-path ./dnn_model --config-file /home/vastbase_ai/local/vastbase_11395/bin/dbmind/components/sqldiag/sqldiag.conf --predicted-file ./result/t_result