DLA SQL分析函数:SQL语句审计与分析的利器

1. 简介

Data Lake Analytics(https://www.aliyun.com/product/datalakeanalytics)最新release一组SQL内置函数,用来进行SQL语句的分析、信息提取,方便用户对SQL语句进行语法层面的审计、分析,可以应用于很多安全、数据库日志分析等场景。参考DLA(Data Lake Analytics)之前的文档,只要您的SQL语句文本保存在DLA支持的数据源系统中,就能使用DLA的SQL分析函数,方便、快捷的进行SQL语句的审计和分析,或者基于此构建相关应用程序和系统。

前提条件,您的SQL语句文本已经作为一个字符串、文本字段存储在DLA支持的数据源中,或者在统一格式的日志文件中,日志文件放到OSS中(应用自己上传、开源日志工具上传、日志服务投递(https://help.aliyun.com/document_detail/107980.html)、Datahub投递等),然后采用DLA进行SQL分析。

2. SQL分析函数详解

所有提供的SQL分析函数都是scalar标量函数。SQL分析函数支持多种SQL方言,默认SQL方言是mysql。

SQL方言
mysql
postgresql
oracle
db2
sqlserver
hive
odps

支持的函数列表:

Name Description
sql_export_columns 提取SQL语句中所有出现的列
sql_export_functions 提取SQL语句中所有出现的函数
sql_export_predicate_columns 提取SQL语句中所有出现的谓词条件表达式涉及的列
sql_export_predicates 提取SQL语句中所有出现的谓词条件表达式
sql_export_select_list_columns 提取SQL语句中SELECT子句表达式中出现的列(包括子查询)
sql_export_tables 提取SQL语句中所有出现的表
sql_format 对SQL语句进行格式化
sql_params 提取SQL语句中所有的literal值
sql_pattern 提取SQL语句参数化后的SQL pattern,literal用?代替
sql_pattern_hash 提取sqlText的SQL语句参数化后的SQL pattern,并生成hash值
sql_syntax_check 对sqlText进行语法检查

2.1 sql_export_columns

函数调用形式:

  • sql_export_columns(sqlText)
  • sql_export_columns(sqlText, dbType)
  • sql_export_columns(sqlText, dbType, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
VARCHAR sqlText的SQL语句中所有出现的列,用逗号分隔,列所属的表会进行关联推导,如果没有找到明确的表,则为UNKNOWN

示例:

SELECT sql_export_columns(a.sql_text)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_export_columns(a.sql_text)                                                                                                                                                                                                                                                                                                                  |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| UNKNOWN.s_acctbal,UNKNOWN.s_name,UNKNOWN.n_name,UNKNOWN.p_partkey,UNKNOWN.p_mfgr,UNKNOWN.s_address,UNKNOWN.s_phone,UNKNOWN.s_comment,UNKNOWN.ps_partkey,UNKNOWN.s_suppkey,UNKNOWN.ps_suppkey,UNKNOWN.p_size,UNKNOWN.p_type,UNKNOWN.s_nationkey,UNKNOWN.n_nationkey,UNKNOWN.n_regionkey,UNKNOWN.r_regionkey,UNKNOWN.r_name,UNKNOWN.ps_supplycost |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

2.2 sql_export_functions

函数调用形式:

  • sql_export_functions(sqlText)
  • sql_export_functions(sqlText, dbType)
  • sql_export_functions(sqlText, dbType, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
VARCHAR sqlText的SQL语句中所有出现的函数名,用逗号分隔,列所属的表会进行关联推导,如果没有找到明确的表,则为UNKNOWN

示例:

SELECT sql_export_functions(a.sql_text)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+----------------------------------+
| sql_export_functions(a.sql_text) |
+----------------------------------+
| min                              |
+----------------------------------+

2.3 sql_export_predicate_columns

函数调用形式:

  • sql_export_predicate_columns(sqlText)
  • sql_export_predicate_columns(sqlText, dbType)
  • sql_export_predicate_columns(sqlText, dbType, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
VARCHAR sqlText的SQL语句中出现在谓词条件表达式中的列名,用逗号分隔,列所属的表会进行关联推导,如果没有找到明确的表,则为UNKNOWN

示例:

SELECT sql_export_predicate_columns(a.sql_text)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_export_predicate_columns(a.sql_text)                                                                                                                                                                                     |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| UNKNOWN.p_partkey,UNKNOWN.ps_partkey,UNKNOWN.s_suppkey,UNKNOWN.ps_suppkey,UNKNOWN.p_size,UNKNOWN.p_type,UNKNOWN.s_nationkey,UNKNOWN.n_nationkey,UNKNOWN.n_regionkey,UNKNOWN.r_regionkey,UNKNOWN.r_name,UNKNOWN.ps_supplycost |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

2.4 sql_export_predicates

函数调用形式:

  • sql_export_predicates(sqlText)
  • sql_export_predicates(sqlText, dbType)
  • sql_export_predicates(sqlText, dbType, compactValues)
  • sql_export_predicates(sqlText, dbType, compactValues, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
compactValues BOOLEAN 可选。true时,谓词条件中值以数组的形式出现在返回值中
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
VARCHAR sqlText的SQL语句中所有出现的谓词条件表达式元素数组,用逗号分隔,列所属的表会进行关联推导,如果没有找到明确的表,则为UNKNOWN

示例:

SELECT sql_export_predicates(a.sql_text, ‘mysql‘, true)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_export_predicates(a.sql_text)                                                                                                                                                                                                                                                                                                                                                                                                                  |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [["UNKNOWN","p_partkey","=",null],["UNKNOWN","ps_partkey","=",null],["UNKNOWN","s_suppkey","=",null],["UNKNOWN","ps_suppkey","=",null],["UNKNOWN","p_size","=",35],["UNKNOWN","p_type","LIKE","%NICKEL"],["UNKNOWN","s_nationkey","=",null],["UNKNOWN","n_nationkey","=",null],["UNKNOWN","n_regionkey","=",null],["UNKNOWN","r_regionkey","=",null],["UNKNOWN","r_name","=",["MIDDLE EAST","MIDDLE EAST"]],["UNKNOWN","ps_supplycost","IN",null]] |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

2.5 sql_export_select_list_columns

函数调用形式:

  • sql_export_select_list_columns(sqlText)
  • sql_export_select_list_columns(sqlText, dbType)
  • sql_export_select_list_columns(sqlText, dbType, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
VARCHAR sqlText的SQL语句中SELECT子句返回列中出现的列名列表(包括子查询),用逗号分隔,列所属的表会进行关联推导,如果没有找到明确的表,则为UNKNOWN

示例:

SELECT sql_export_select_list_columns(a.sql_text)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_export_select_list_columns(a.sql_text)                                                                                                                 |
+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| UNKNOWN.s_acctbal,UNKNOWN.s_name,UNKNOWN.n_name,UNKNOWN.p_partkey,UNKNOWN.p_mfgr,UNKNOWN.s_address,UNKNOWN.s_phone,UNKNOWN.s_comment,UNKNOWN.ps_supplycost |
+------------------------------------------------------------------------------------------------------------------------------------------------------------+

2.6 sql_export_tables

函数调用形式:

  • sql_export_tables(sqlText)
  • sql_export_tables(sqlText, dbType)
  • sql_export_tables(sqlText, dbType, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
VARCHAR sqlText的SQL语句中所有出现的表名,用逗号分隔

示例:

SELECT sql_export_tables(a.sql_text)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+--------------------------------------+
| sql_export_tables(a.sql_text)        |
+--------------------------------------+
| part,supplier,partsupp,nation,region |
+--------------------------------------+

2.7 sql_format

函数调用形式:

  • sql_format(sqlText)
  • sql_format(sqlText, dbType)
  • sql_format(sqlText, dbType, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
VARCHAR 格式化sqlText的SQL语句

示例:

SELECT sql_format(a.sql_text)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_format(a.sql_text)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SELECT s_acctbal, s_name, n_name, p_partkey, p_mfgr
    , s_address, s_phone, s_comment
FROM part, supplier, partsupp, nation, region
WHERE p_partkey = ps_partkey
    AND s_suppkey = ps_suppkey
    AND p_size = 35
    AND p_type LIKE ‘%NICKEL‘
    AND s_nationkey = n_nationkey
    AND n_regionkey = r_regionkey
    AND r_name = ‘MIDDLE EAST‘
    AND ps_supplycost IN (
        SELECT min(ps_supplycost)
        FROM partsupp, supplier, nation, region
        WHERE s_suppkey = ps_suppkey
            AND s_nationkey = n_nationkey
            AND n_regionkey = r_regionkey
            AND r_name = ‘MIDDLE EAST‘
    )
ORDER BY s_acctbal DESC, n_name, s_name, p_partkey
LIMIT 100; |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

2.8 sql_params

函数调用形式:

  • sql_params(sqlText)
  • sql_params(sqlText, dbType)
  • sql_params(sqlText, dbType, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
VARCHAR 提取sqlText的SQL语句中的literal值

示例:

SELECT sql_params(a.sql_text)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+------------------------------------------------+
| sql_params(a.sql_text)                         |
+------------------------------------------------+
| [35,"%NICKEL","MIDDLE EAST","MIDDLE EAST",100] |
+------------------------------------------------+

2.10 sql_pattern

函数调用形式:

  • sql_pattern(sqlText)
  • sql_pattern(sqlText, dbType)
  • sql_pattern(sqlText, dbType, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
VARCHAR 提取sqlText的SQL语句参数化后的SQL pattern, literal值换成?

示例:

SELECT sql_pattern(a.sql_text)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sql_pattern(a.sql_text)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SELECT s_acctbal, s_name, n_name, p_partkey, p_mfgr
    , s_address, s_phone, s_comment
FROM part, supplier, partsupp, nation, region
WHERE p_partkey = ps_partkey
    AND s_suppkey = ps_suppkey
    AND p_size = ?
    AND p_type LIKE ?
    AND s_nationkey = n_nationkey
    AND n_regionkey = r_regionkey
    AND r_name = ?
    AND ps_supplycost IN (
        SELECT min(ps_supplycost)
        FROM partsupp, supplier, nation, region
        WHERE s_suppkey = ps_suppkey
            AND s_nationkey = n_nationkey
            AND n_regionkey = r_regionkey
            AND r_name = ?
    )
ORDER BY s_acctbal DESC, n_name, s_name, p_partkey
LIMIT ?; |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

2.11 sql_pattern_hash

函数调用形式:

  • sql_pattern_hash(sqlText)
  • sql_pattern_hash(sqlText, dbType)
  • sql_pattern_hash(sqlText, dbType, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
VARCHAR 提取sqlText的SQL语句参数化后的SQL pattern,并生成hash值

示例:

SELECT sql_pattern_hash(a.sql_text)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+------------------------------+
| sql_pattern_hash(a.sql_text) |
+------------------------------+
|           925870115679910184 |
+------------------------------+

2.12 sql_syntax_check

函数调用形式:

  • sql_syntax_check(sqlText)
  • sql_syntax_check(sqlText, dbType)
  • sql_syntax_check(sqlText, dbType, throwError)

参数说明:

参数名 类型 说明
sqlText VARCHAR 必选
dbType VARCHAR 可选。SQL方言,默认为mysql
throwError BOOLEAN 可选。true时,遇到非法SQL抛出异常;false时,遇到非法SQL不抛出异常,返回null

返回值:

类型 说明
BOOLEAN 对sqlText进行语法检查,1表示正确,0表示错误

示例:

SELECT sql_syntax_check(a.sql_text)
FROM (
SELECT ‘SELECT   s_acctbal,
         s_name,
         n_name,
         p_partkey,
         p_mfgr,
         s_address,
         s_phone,
         s_comment
FROM     part,
         supplier,
         partsupp,
         nation,
         region
WHERE    p_partkey = ps_partkey
AND      s_suppkey = ps_suppkey
AND      p_size = 35
AND      p_type LIKE ‘‘%NICKEL‘‘
AND      s_nationkey = n_nationkey
AND      n_regionkey = r_regionkey
AND      r_name = ‘‘MIDDLE EAST‘‘
AND      ps_supplycost IN
         (
                SELECT min(ps_supplycost)
                FROM   partsupp,
                       supplier,
                       nation,
                       region
                WHERE  s_suppkey = ps_suppkey
                AND    s_nationkey = n_nationkey
                AND    n_regionkey = r_regionkey
                AND    r_name = ‘‘MIDDLE EAST‘‘ )
ORDER BY s_acctbal DESC,
         n_name,
         s_name,
         p_partkey
LIMIT    100;‘ AS sql_text
) a;

+------------------------------+
| sql_syntax_check(a.sql_text) |
+------------------------------+
|                            1 |
+------------------------------+


本文作者:julian.zhou

原文链接

本文为云栖社区原创内容,未经允许不得转载。

原文地址:https://www.cnblogs.com/zhaowei121/p/10757335.html

时间: 2024-10-05 02:22:36

DLA SQL分析函数:SQL语句审计与分析的利器的相关文章

DRDS SQL 审计与分析——全面洞察 SQL 之利器

背景 数据库存储着系统的核心数据,其安全方面的问题在传统环境中已经成为泄漏和被篡改的重要根源.而在云端,数据库所面临的威胁被进一步的放大.因此,对云数据库的操作行为尤其是全量 SQL 执行记录的审计日志,就显得尤为重要,是保障云数据库安全的最基本要求.那么针对云数据库的 SQL 审计,您是否存在如下疑问: SQL 审计对数据库的性能有影响吗? 数据被篡改,但是没启用 SQL 审计,还能追溯篡改者吗? SQL 审计日志除了追溯安全问题外,还能发挥更大的作用吗?能否帮助我们洞察 SQL 的执行状况.

Oracle SQL 语句高版本分析

OracleSQL 语句高版本分析 1.     何为高版本 每次执行一条SQL语句时,如果其对应的当前已经存在于library cache里的一个父游标下的各个子游标都不能被该SQL语句重新使用(即共享),则会产生一个新的子游标,此时就会在V$SQL_SHARED_CURSOR里新增一行,分别描述该SQL语句不能使用当前已经存在的各个子游标的原因.当然一个父游标下的第一个子游标产生时也会在该视图上新增一行,只是各个该视图上的描述原因的列的值都为N,在V$SQL_SHARED_CURSOR行数就

戈多编程-小谈sql语句的优化分析

在sqlserver大数据查询中,避免不了查询效率减慢,暂且抛弃硬件原因和版本原因,仅从sql语句角度分析. 一. sql 语句性能不达标,主要原因有一下几点: 1. 未建索引,检索导致全表扫描 2. 已建索引,但是未走索引导致索引失效,进而全表扫描. 3. 没有有效的索引视图 二. sql 语句优化 1. 分析比较执行时间计划读取情况 (1) 查看执行时间和cpu占用时间和查询对I/O的操作情况 I.先执行一个400多万数据的sql set statistics time,io on sele

python 3 mysql sql逻辑查询语句执行顺序

python 3 mysql sql逻辑查询语句执行顺序 一 .SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN <right_table> ON <join_condition> WHERE <where_condition> GROUP BY <group_by_list> HAVING <havin

【MS SQL】通过执行计划来分析SQL性能

如何知道一句SQL语句的执行效率呢,只知道下面3种: 1.通过SQL语句执行时磁盘的活动量(IO)信息来分析:SET STATISTICS IO ON (开启) / SET STATISTICS IO OFF (关闭) 2.通过SQL语句执行时语法分析.编译以及执行所消耗的时间:SET STATISTICS TIME ON (开启) / SET STATISTICS TIME OFF (关闭) 3.通过执行计划查看:Ctrl + L -------------------------------

45、SQL逻辑查询语句执行顺序

一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN <right_table> ON <join_condition> WHERE <where_condition> GROUP BY <group_by_list> HAVING <having_condition> ORDER BY <o

MySQL 抓取SQL慢查询语句

当MySQL服务器出现异常(慢),首先要考虑是否因,SQL语句引起数据库慢,如果情况比较紧急,我们就要立刻 SHOW FULL PROCESSLIST; 去查看,但我建议大家使用-e参数,采用非交互的方式,因为这样可以使用grep等命令,对结果进行过滤,更方便直观的看到结果 一.抓SQL慢查询语句的方法,有2种: 1,临时紧急抓取 通过SHOW FULL PROCESSLIST; 的方式,执行几次,有相同语句,就可能是SQL慢查询语句: SHOW FULL PROCESSLIST; #查看MyS

对SQL Server SQL语句进行优化的10个原则

1.使用索引来更快地遍历表. 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的.在非群集索引下,数据在物理上随机存放在数据页上.合理的索引设计要建立在对各种查询的分析和预测上.一般来说:①.有大量重复值.且经常有范围查询(between, >,< ,>=,< =)和order by.group by发生的列,可考虑建立群集索引:②.经常同时存取多列,且每列都含有重复值可考虑建立组合索引:③.组合索引要尽量使关键查询形成索引覆盖,其前导列一定是使用最频繁的列. 2.IS NUL

mysql五补充部分:SQL逻辑查询语句执行顺序

阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN <right_table> ON <join_condition> WHERE <where_condition>