Identifying SQL Statements for Later Plan Retrieval?? 标示SQL语句便于之后计划检索 (page 171)
??? If you want to retrieve a statement that was executed in the past, you can retrieve the SQL_ID and
CHILD_NUMBER from V$SQL as demonstrated in Listing 6-7.? To simplify finding the correct statement
identifiers, especially when I’m testing, I add a unique comment that identifies each statement I
execute.? Then, whenever I want to grab that plan from the library cache, all I have to do is query V$SQL to locate the statement text that includes the comment I used.? Listing 6-11 shows an example of this and the query I use to subsequently find the statement I want.?
??? 如果你想要检索一条之前执行过的语句,你可像列表6-7示例的,查询V$SQL的SQL_ID和CHILD_NUMBER列。为了容易找出正确的语句标示,尤其在我测试时,在每条语句上加上唯一的注释。这样无论何时,我要从库缓存中取出计划,所要做的只是查询V$SQL,定位包含我所使用的注释文本的语句。列表6-11展示了这个例子和之后我要找出想要的语句的查询。
Listing 6-11. Using a Comment to Uniquely Identify a SQL Statement?? 用唯一注释标示SQL语句
SQL>select /* KM-EMPTEST1 */
? 2??????? empno, ename
? 3?? from emp
? 4? where job = 'MANAGER' ;
????? EMPNO ENAME
---------- ----------
????? 7566 JONES
????? 7698 BLAKE
????? 7782 CLARK
?
SQL>select sql_id, child_number, sql_text
? 2? from v$sql
? 3? where sql_text like '%KM-EMPTEST1%';
?
SQL_ID???????????????????? CHILD_NUMBER?????????????????????? SQL_TEXT
-------------???????????????? ------------??????????????????????????????????? -------------------------------------------
9qu1dvthfcqsp??????????? 0????????????????????????????????????????????? select /* KM-EMPTEST1 */???? empno, ename??
??????????????????????????????????????????????????????????????????????????????????? from emp where job = 'MANAGER'
a7nzwn3t522mt?????????? 0???????????????????????????????????????????? select sql_id, child_number, sql_text from?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? v$sql where sql_text like '%KM-EMPTEST1%'
?
SQL>select * from table(dbms_xplan.display_cursor('9qu1dvthfcqsp',0,'ALLSTATS LAST'));
?
PLAN_TABLE_OUTPUT
--------------------------------------------------
SQL_ID? 9qu1dvthfcqsp, child number 0
-------------------------------------
select /* KM-EMPTEST1 */???? empno, ename? from emp where job =
'MANAGER'
?
Plan hash value: 3956160932
------------------------------------------------
| Id? | Operation????????????????????? ?? | Name | Starts | E-Rows | A-Rows |?? A-Time?????? | Buffers |
------------------------------------------------
|?? 0 | SELECT STATEMENT??????? |?????????? |????? 1??? |?????? ? ? ?? |????? 3 ? ?? |00:00:00.01? |?????? 8???? |
|*? 1 |? TABLE ACCESS FULL???? | EMP??? |????? 1 ?? |????? 3?????? |????? 3????? |00:00:00.01? |?????? 8???? |
------------------------------------------------
?
Predicate Information (identified by operation id):
---------------------------------------------------
?
?? 1 - filter("JOB"='MANAGER')
??? You’ll notice that when I queried V$SQL, two statements showed up.? One was the SELECT statement I was executing to find the entry in V$SQL and one was the query I executed.? While this set of steps gets the job done, I find it easier to automate the whole process into a single script.? In that script, I find the statement I want in V$SQL by weeding out the query I’m running to find it and also by ensuring that I find the most recently executed statement that uses my identifying comment.? Listing 6-12 shows the script I use in action.
??? 如你所见,我查询V$SQL展现了两条语句。一条SELECT语句是我执行查询V$SQL的,一条是我之前执行的查询。既然这套步骤有效,我发现用一个脚本自动化处理整个过程更爽。在这个脚本中,我找出想要的语句,清除查询V$SQL的那条语句,确保找到最近执行的且使用注释标示的语句。列表6-12展示了这个脚本的使用过程。
Listing 6-12. Automating Retrieval of an Execution Plan for any SQL Statement
SQL>select /* KM-EMPTEST2 */
? 2???????? empno, ename
? 3??? from emp
? 4?? where job = 'CLERK' ;
?
???? EMPNO ENAME
---------- ----------
????? 7369 SMITH
????? 7876 ADAMS
????? 7900 JAMES
????? 7934 MILLER
SQL>
SQL>get pln.sql
? 1? SELECT xplan.*
? 2? FROM
? 3???? (
? 4???? select max(sql_id) keep
? 5??????????? (dense_rank last order by last_active_time) sql_id
? 6????????? , max(child_number) keep
? 7??????????? (dense_rank last order by last_active_time) child_number
? 8?????? from v$sql
? 9????? where upper(sql_text) like '%&1%'
?10??????? and upper(sql_text) not like '%FROM V$SQL WHERE UPPER(SQL_TEXT) LIKE %'
?11????? ) sqlinfo,
?12???? table(DBMS_XPLAN.DISPLAY_CURSOR(sqlinfo.sql_id, sqlinfo.child_number, 'ALLSTATS
LAST')) xplan
?13* /
?
SQL>@pln KM-EMPTEST2
?
PLAN_TABLE_OUTPUT
----------------------------------------------------SQL_ID?
bn37qcafkwkt0, child number 0
-------------------------------------
select /* KM-EMPTEST2 */??????? empno, ename?? from emp? where job =
'CLERK'
Plan hash value: 3956160932
?
------------------------------------------------
| Id? | Operation??????????????????????????? ? ? | Name | Starts | E-Rows | A-Rows??????? |?? A-Time?? | Buffers |
------------------------------------------------
|?? 0 | SELECT STATEMENT? |?????????? |????? 1 ? |?????? ? ?? |????? 4???? |00:00:00.01? |?????? 8?????? |
|*? 1 |? TABLE ACCESS FULL| EMP ? |????? 1?? |????? 3 ? |????? 4????? |00:00:00.01? |?????? 8??????? |
------------------------------------------------
?
Predicate Information (identified by operation id):
---------------------------------------------------
?
?? 1 - filter("JOB"='CLERK')
??? This script will return the execution plan associated with the most recently executed SQL
statement that matches the pattern you enter.? As I mentioned, it is easier to find a statement if you’ve made an effort to use a comment to identify it, but it will work to find any string of matching text you enter.? However, if there are multiple statements with matching text, this script will only display the most recently executed statement matching the pattern.? If you want a different statement, you’ll have to issue a query against V$SQL such as the one in Listing 6-11 and then feed the correct SQL_ID and CHILD_NUMBER to the dbms_xplan.display_cursor call.
??? 这个脚本将返回,关联最近执行的SQL语句的,匹配你输入(文本)模式的执行计划。正如我所提到的,如果你精心使用一注释标识一语句,你将很容易找到它。但是它也能找出任何匹配你输入文本串(的执行计划)。然而,如果有多个语句匹配(你输入的文本),脚本只返回最近执行的匹配语句。如果你想要不同的语句,你必须查询V$SQL,像列表6-11那样。然后填上正确的SQL_ID和CHILD_NUMBER调用dbms_xplan.display_curor。
详细解决方案
《Pro Oracle SQL》-Chapter 6-6.2 Execute Plans-之三
热度:385 发布时间:2016-05-05 14:32:40.0
相关解决方案
- oracle 分页排序,ssi,该怎么处理
- oracle 最近的时间(而不是前一天的时间) 跪求sql语句 。解决方法
- struts2中 super,super.execute();是什么意思,如何理解
- oracle,该怎么解决
- 在 Hibernate3 查询不到 Oracle 11g 里的记录
- oracle 安装时出现 java tm 异常
- android访问其他数据库(如:oracle、MySql等),希望大家给点建议!解决方案
- oracle 安插 LONG VARCHAR 类型数据
- ORACLE 一条SQL的有关问题
- oracle loadjava如何用
- oracle,该如何处理
- could not execute query 错误
- C# + SQL server +oracle QQ交流群142703980解决方法
- 怎么跟踪winform应用程序发送到数据库的sql(oracle、mssql)
- asp.net 中有没有相仿 execute "Response.Write 0" 这样的命令
- , 每次玩ASP都会遇到一些恶心的有关问题, 这次是:未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项
- nhibernate [could not execute query]错误
- 尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运,该怎么解决
- 求解:gridview行值的计算并绑定有关问题(asp.net+oracle)
- Oracle.DataAccess 执行多条sql语句,该如何解决
- Could not execute CVTRES.EXE [跪求解决方案!]解决方法
- VS10连接Oracle的有关问题:"Oracle.DataAccess.Client.OracleConnection"的类型初始值设定项引发错误
- System.Data.OleDb.OleDbException: 未找到 Oracle 客户端和网络组件。
- asp.net oracle 参数化 模糊查询出错,该怎么处理
- Oracle-参数查询有关问题
- oracle,insert 语句插入一条记录后要求,返回主键id値。解决思路
- Oracle 10g下的SQL语句,有点难度.请~[同时向朋友们道歉~]
- 月统计(oracle)解决办法
- 在线紧急等一sql语句(oracle),该怎么处理
- 面试问,为什么jsp+oracle,asp+sql server这样来搭配?该如何处理