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

简介: 1. 简介 Data Lake Analytics(https://www.aliyun.com/product/datalakeanalytics)最新release一组SQL内置函数,用来进行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 |
+------------------------------+
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
23天前
|
SQL Oracle 关系型数据库
SQL优化-使用联合索引和函数索引
在一次例行巡检中,发现一条使用 `to_char` 函数将日期转换为字符串的 SQL 语句 CPU 利用率很高。为了优化该语句,首先分析了 where 条件中各列的选择性,并创建了不同类型的索引,包括普通索引、函数索引和虚拟列索引。通过对比不同索引的执行计划,最终确定了使用复合索引(包含函数表达式)能够显著降低查询成本,提高执行效率。
|
29天前
|
SQL 存储 数据可视化
手机短信SQL分析技巧与方法
在手机短信应用中,SQL分析扮演着至关重要的角色
|
29天前
|
SQL 数据库 数据库管理
数据库SQL函数应用技巧与方法
在数据库管理中,SQL函数是处理和分析数据的强大工具
|
30天前
|
SQL 数据库 索引
SQL中COUNT函数结合条件使用的技巧与方法
在SQL查询中,COUNT函数是一个非常常用的聚合函数,用于计算表中满足特定条件的记录数
|
30天前
|
SQL 关系型数据库 MySQL
SQL日期函数
SQL日期函数
|
2月前
|
SQL 关系型数据库 C语言
PostgreSQL SQL扩展 ---- C语言函数(三)
可以用C(或者与C兼容,比如C++)语言编写用户自定义函数(User-defined functions)。这些函数被编译到动态可加载目标文件(也称为共享库)中并被守护进程加载到服务中。“C语言函数”与“内部函数”的区别就在于动态加载这个特性,二者的实际编码约定本质上是相同的(因此,标准的内部函数库为用户自定义C语言函数提供了丰富的示例代码)
|
3月前
|
SQL 数据处理 数据库
|
3月前
|
前端开发 Java JSON
Struts 2携手AngularJS与React:探索企业级后端与现代前端框架的完美融合之道
【8月更文挑战第31天】随着Web应用复杂性的提升,前端技术日新月异。AngularJS和React作为主流前端框架,凭借强大的数据绑定和组件化能力,显著提升了开发动态及交互式Web应用的效率。同时,Struts 2 以其出色的性能和丰富的功能,成为众多Java开发者构建企业级应用的首选后端框架。本文探讨了如何将 Struts 2 与 AngularJS 和 React 整合,以充分发挥前后端各自优势,构建更强大、灵活的 Web 应用。
55 0
|
3月前
|
SQL Oracle 关系型数据库
SQL 中的大小写处理函数详解
【8月更文挑战第31天】
122 0
|
3月前
|
SQL 数据采集 数据挖掘
为什么要使用 SQL 函数?详尽分析
【8月更文挑战第31天】
47 0
下一篇
无影云桌面