如何得到给定SQL的SQL_ID
本文参考盖国强老师的博文进行测试。
文章名称:如何得到给定SQL的SQL_ID
文章源于:http://azwzsj.com/article/iesdhs.html
作者:eygle |English 【转载时请标明出处和作者信息】|【恩墨学院 OCM培训传DBA成功之道】
链接:http://www.eygle.com/archives/2017/08/dbms_sqltune_util0_sqltext_to_sqlid.html
-------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
-
在 Oracle 数据库中,如何得到给定SQL的 SQL_ID ? 这是曾经被广泛讨论的一个问题。
-
现在,在Oracle 11g中,Oracle 给出了一个系统包,通过 dbms_sqltune_util0 可以简便的计算出给定SQL的SQL_ID。
-
-
SQL> desc dbms_sqltune_util0
-
FUNCTION EXTRACT_BIND RETURNS SQL_BIND
-
Argument Name Type In/Out Default?
-
------------------------------ ----------------------- ------ --------
-
BIND_DATA RAW IN
-
BIND_POS BINARY_INTEGER IN
-
FUNCTION EXTRACT_BINDS RETURNS SQL_BIND_SET
-
Argument Name Type In/Out Default?
-
------------------------------ ----------------------- ------ --------
-
BIND_DATA RAW IN
-
FUNCTION GET_BINDS_COUNT RETURNS BINARY_INTEGER
-
Argument Name Type In/Out Default?
-
------------------------------ ----------------------- ------ --------
-
BIND_DATA RAW IN
-
FUNCTION IS_BIND_MASKED RETURNS NUMBER
-
Argument Name Type In/Out Default?
-
------------------------------ ----------------------- ------ --------
-
BIND_POS BINARY_INTEGER IN
-
MASKED_BINDS_FLAG RAW IN DEFAULT
-
FUNCTION SQLTEXT_TO_SIGNATURE RETURNS NUMBER
-
Argument Name Type In/Out Default?
-
------------------------------ ----------------------- ------ --------
-
SQL_TEXT CLOB IN
-
FORCE_MATCH BINARY_INTEGER IN DEFAULT
-
FUNCTION SQLTEXT_TO_SQLID RETURNS VARCHAR2
-
Argument Name Type In/Out Default?
-
------------------------------ ----------------------- ------ --------
-
SQL_TEXT CLOB IN
-
FUNCTION VALIDATE_SQLID RETURNS BINARY_INTEGER
-
Argument Name Type In/Out Default?
-
------------------------------ ----------------------- ------ --------
-
SQL_ID VARCHAR2 IN
-
-
函数 sqltext_to_sqlid 用于实现这个功能,以下测试使用了一个简单的SQL查询。
-
注意Oracle在SQL最后加入一个 chr(0) 的不可见字符,我们需要补齐:
-
-
SYS@ r7>SELECT DBMS_SQLTUNE_UTIL0.SQLTEXT_TO_SQLID('SELECT SYSDATE FROM DUAL'||CHR(0)) SQL_ID FROM DUAL;
-
-
SQL_ID
-
----------------------------------------------------------------------------------------------------------------------------------------------------------------
-
c749bc43qqfz3
-
接下来看一下执行这个查询,数据库中自动生成的SQL_ID,与通过函数转换生成的完全一致:
-
SYS@ r7>SELECT SYSDATE FROM DUAL;
-
-
SYSDATE
-
---------
-
21-AUG-17
-
-
SYS@ r7>SELECT SQL_ID FROM V$SQL WHERE SQL_TEXT='SELECT SYSDATE FROM DUAL';
-
-
SQL_ID
-
-------------
-
c749bc43qqfz3
-
-
查看执行计划
-
SYS@ r7>select * from table(dbms_xplan.display_cursor('c749bc43qqfz3'));
-
-
PLAN_TABLE_OUTPUT
-
----------------------------------------------------------------------------------------------------------------------------------------------------------------
-
SQL_ID c749bc43qqfz3, child number 0
-
-------------------------------------
-
SELECT SYSDATE FROM DUAL
-
Plan hash value: 1388734953
-
-
-----------------------------------------------------------------
-
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-
-----------------------------------------------------------------
-
| 0 | SELECT STATEMENT | | | 2 (100)| |
-
| 1 | FAST DUAL | | 1 | 2 (0)| 00:00:01 |
-
-----------------------------------------------------------------
-
- 13 rows selected.
文章名称:如何得到给定SQL的SQL_ID
文章源于:http://azwzsj.com/article/iesdhs.html