`

《Pro Oracle SQL》--Chapter 6 SQL Execution Plans --6.2 Execution Plans--之一

阅读更多

Execution Plans   执行计划 (page 166)
     The actual execution plan for a SQL statement is produced when a statement is executed.  After the statement is hard parsed, the plan that is chosen is stored in the library cache for later reuse.  The plan operations can be viewed by querying V$SQL_PLAN.  V$SQL_PLAN has basically the same definition as the PLAN_TABLE except that it has several columns that contain the information on how to identify and find the statement in the library cache.  These additional columns are: ADDRESS, HASH_VALUE, SQL_ID, PLAN_HASH_VALUE, CHILD_ADDRESS, and CHILD_NUMBER.  You can find any SQL statement using one or more of these values.  
    SQL 语句的实际执行计划是在语句执行时生成的。在语句硬解析之后,被选择的计划存储在库缓存(library cache)中以备重用。可以通过V$SQK_PLAN查看计划操作。V$SQK_PLAN的基本定义于PLAN_TABLE相同,除了它有几列包含如何 标示和找出在库缓存中语句的信息。这些额外的列  是:ADDRESS,HASH_VALUE,SQL_ID,PLAN_HASH_VALUE,CHILD_ADDRESS,和CHILD_NUMBER。 你将会发现任何SQL语句或多或少使用了这些值。
Viewing Recently Generated SQL   查看最近生成的SQL
Listing 6-7 shows a query against V$SQL for recently executed SQL for the SCOTT user and the
identifying values for each column.
列表6-7展示对V$SQL的查询,获取SCOTT用户最近执行的SQL,标示了每列的值。
Listing 6-7. V$SQL Query to Get Recently Executed SQL    
SQL>select /* recentsql */ sql_id, child_number, hash_value, address, executions, sql_text
  2    from v$sql
  3   where parsing_user_id = (select user_id
  4                              from all_users
  5                             where username = 'SCOTT')
  6     and command_type in (2,3,6,7,189)
  7     and UPPER(sql_text) not like UPPER('%recentsql%')
  8  /
 
SQL_ID                CHILD_NUMBER       HASH_VALUE ADDRESS    EXECUTIONS   SQL_TEXT
----------------------      ----------        -------------------------------      ------------     --------------------
g5wp7pwtq4kwp            0            862079893 3829AE54                1             select * from emp
1gg46m60z7k2p            0             2180237397 38280AD0             1              select * from bonus
4g0qfgmtb7z70             0             4071881952 38281D68             1              select * from dept
 
3 rows selected.

    After connecting as user SCOTT, you execute the three queries shown.  Then, when you run the
query against V$SQL, you can see that they are now loaded into the library cache and each has
identifiers associated with it.  The SQL_ID and CHILD_NUMBER columns contain the identifying
information that you’ll use most often to retrieve a statement’s plan and execution statistics.  
    以SCOTT用户连接,执行上面三个查询。然后再查V$SQL,你将看到它们已经加载到库缓存中了,且每一个都有唯一的标示关联着它们。SQL_ID和CHILD_NUMBER列包含标示信息,你将经常使用它们去检索一条语句的计划和执行的统计。

Viewing the Associated Execution Plan   查看相关的执行计划
    There are several ways to view the execution plan for any SQL statement that has been previously
executed and still remains in the library cache.  The easiest way is to use the
dbms_xplan.display_cursor function.  Listing 6-8 shows how to use dbms_xplan.display_cursor to show the execution plan for the most recently executed SQL statement. 
     有几种方法查看之前执行的任何SQL语句(所生成的)还保留在库缓存中的执行计划。最简单的方式就是使用 dbms_xplan.display_curor函数。列表6-8展现了如何使用dbms_xplan.display_cursor展示最近执行 SQL语句的执行计划。
     Listing 6-8. Using  dbms_xplan.display_cursor
SQL>select /*+ gather_plan_statistics */ empno, ename from scott.emp where ename = 'KING' ;
 
     EMPNO ENAME
---------- ----------
      7839 KING
SQL>
SQL>set serveroutput off
SQL>select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));
 
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------
SQL_ID  2dzsuync8upv0, child number 0
-------------------------------------
select empno, ename from scott.emp where ename = 'KING'
 
Plan hash value: 3956160932
 
------------------------------------------------------------------------------------
| Id  | Operation                           | Name | Starts | E-Rows | A-Rows |   A-Time      | Buffers |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |           |      1   |               |      1      |00:00:00.01  |       8    |
|*  1 |    TABLE ACCESS FULL       | EMP    |      1   |      1       |      1      |00:00:00.01   |       8    |
------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   1 - filter("ENAME"='KING')

     First, note the use of the gather_plan_statistics hint in the query.  In order to capture rowsource
execution statistics for the plan, you must tell Oracle to gather this information as the statement
executes.  The rowsource execution statistics include the number of rows, number of consistent reads,
number of physical reads, number of physical writes, and the elapsed time for each operation on a row.  This information can be gathered using this hint on a statement-by-statement basis, or you can set the STATISTICS_LEVEL instance parameter to ALL.  Capturing these statistics does add some overhead to the execution of a statement and so you may not want to have it “always on.”  The hint allows you to use it when you need to—and only for the individual statements you choose.  The presence of this hint collects the information and shows it in the Starts, A-Rows, A-Time, and Buffers columns.  Listing 6-9 shows how the plan output would appear if you didn’t use the hint (or set the parameter value to ALL).
      首先,注意在查询中gather_plan_statistics提示的使用。为了获取计划的行源的执行统计(信息),你必须告诉Oracle在语句执行 时收集这些信息。行源的执行统计(信息)包括行数,一致读(逻辑读)数,物理读数,物理写数,和在每行上每步操作耗费的时间。通过使用这种建立在语句基础上的语句提示,或者你设定STATISTICS_LEVEL实例参数为ALL,就能收集这些信息。捕获这些统计加重了执行语句的开销,所以你可能不想要它 “一直开着”。提示允许你在需要的时候才用它,且只对你所选的单个语句。(在语句中)提示的呈现将收集信息,在列Start,A-Rows,A- Time,和Buffers展现。列表6-9展现了如果你不使用提示(或者设定参数为ALL)计划将怎样输出。
Listing 6-9. Using  dbms_xplan.display_cursor without the gather_plan_statistics hint
SQL>select ename from scott.emp where ename = 'KING' ;
 
ENAME
----------
KING
 
SQL>select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));
 
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
SQL_ID  dgvds8td66zvk, child number 1
-------------------------------------
select ename from scott.emp where ename = 'KING'
 
Plan hash value: 3956160932
 
-------------------------------------------
| Id  | Operation                          | Name     | E-Rows |
-------------------------------------------
|   0 | SELECT STATEMENT           |               |               |
|*  1 |  TABLE ACCESS FULL        | EMP        |      1       |
-------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   1 - filter("ENAME"='KING')

Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level
 
As you can see, a Note is displayed indicating that the plan statistics aren’t available and tells you
what to do to collect them.  
如你所见,显示了一提示(Note)指出计划统计信息不是有效的,还告诉你如何收集他们。

2
1
分享到:
评论

相关推荐

    《Pro Oracle SQL》 读书笔记--Chapter 6--6.2 Execution Plans--之四

    《Pro Oracle SQL》是Oracle数据库查询优化的经典之作,第六章主要聚焦在Execution Plans(执行计划)上,这是数据库查询性能优化的关键。本章节的第四部分深入探讨了如何理解和解析执行计划,以及它对SQL性能的影响...

    《Pro Oracle SQL》Chapter10 -- 10.2 Optimizing SQL -10.2.1Testing Execution Plans

    《Pro Oracle SQL》一书的第10章深入探讨了SQL优化,特别是10.2节,重点关注如何测试执行计划,这是SQL性能调优的关键环节。在这个部分,作者旨在帮助读者理解如何有效地评估和改进SQL查询的性能,以提高数据库系统...

    Pro Oracle SQL

    Pro Oracle SQL also helps you minimize parsing overhead, read execution plans, test for correct results, and exert control over SQL execution in your database. You’ll learn when to create indexes, ...

    oracledb-exporter.0.2.9-ora18.5.linux-amd64.tar.gz

    - 查询执行时间(如SQL_EXECUTION_TIME) - 事务处理情况(如COMMIT, ROLLBACK) - 缓冲区命中率(如DB BUFFER HIT) - 锁等待情况(如ENQ: TX - ROW LOCK contention) Prometheus会定期抓取oracledb-exporter提供...

    DBA对Oracle SQL编写规范的总结

    ### DBA对Oracle SQL编写规范的总结 #### 一、引言 在Oracle数据库开发过程中,遵循一套标准化的SQL编写规范对于提升代码质量、增强可读性和可维护性至关重要。本文档由一位经验丰富的数据库管理员(DBA)撰写,旨在...

    Oracle SQL Tuning with Oracle SQLTXPLAIN: Oracle Database 12c Edition

    Oracle SQL Tuning with Oracle SQLTXPLAIN: Oracle Database 12c Edition by Stelios Charalambides English | 8 May 2017 | ISBN: 1484224353 | 408 Pages | PDF | 24.39 MB Learn through this practical guide ...

    Oracle-Pl-Sql.rar

    Oracle PL/SQL是一种强大的编程语言,它将关系数据库管理系统的强大功能与结构化查询语言(SQL)相结合,为开发高效、可靠的企业级应用程序提供了坚实的基础。Oracle PL/SQL的全称是Procedural Language/Structured ...

    oracle-Execution_plan脚本运行配置-010.doc

    ### Oracle 执行计划(Execution Plan)配置与管理 #### 一、概述 Oracle数据库中的执行计划是指数据库系统根据SQL查询语句所选择的最佳查询路径。它对于提高查询效率至关重要。本文将详细介绍如何通过创建索引、...

    ORACLE并行SQL指南

    在Oracle数据库中,并行SQL(Parallel SQL)是一种能够显著提升SQL语句执行效率的技术。传统的串行SQL执行方式中,一个会话只能利用一个CPU核心或者一个磁盘设备进行操作,这意味着即使在拥有多个处理器或磁盘资源的...

    sql-server-2016-samples,SQL SERVER 2016 脚本例子

    在SQL Server 2016中,数据库管理系统(DBMS)提供了一系列强大的功能,使得数据管理、查询和分析更为高效。这个"sql-server-2016-samples"压缩包包含的是针对SQL Server 2016的一系列示例脚本,是学习和理解SQL ...

    pl-sql-dev-8.rar

    PL/SQL是Oracle数据库系统中的一个关键组成部分,它是一种结合了SQL和过程编程的语言,用于在Oracle环境中编写存储过程、函数、触发器等数据库管理任务。"pl-sql-dev-8.rar"是一个压缩包,包含了PL/SQL Developer的...

    Oracle的PL-SQL编程手册

    PL/SQL,全称为Procedural Language/Structured Query Language,是Oracle数据库支持的一种过程化语言,它结合了SQL的强大查询能力与结构化编程语言的特点,提供了丰富的控制结构和异常处理机制。 一、PL-SQL基本...

    SQL Server Execution Plans Second Edition

    Execution plans show you what's going on behind the scenes in SQL Server. They can provide you with a wealth of information on how SQL Server is executing your queries, including the points below. • ...

    高清彩版 dissecting sqlserver execution plans

    标题:“高清彩版 dissecting sqlserver execution plans”(高清彩版剖析SQL Server执行计划),描述:“dissecting sqlserver execution plans”(剖析SQL Server执行计划),标签:oracle。这些信息表明,本文是...

    Oracle-DSI.rar_Oracle-DSI_oracle_oracle dsi_oracle pdf

    8. **Adaptive Execution Plans**:Oracle 11gR2能够根据执行过程中的实际情况动态调整执行计划,以适应不断变化的系统环境。 9. **PGA Auto-Management**:自动管理程序全局区域(PGA)使得内存分配更加智能化,...

    Re-post: DBMS_XPLAN : Display Oracle Execution Plans

    标题“DBMS_XPLAN: 显示Oracle执行计划”是一个关于Oracle数据库系统中用于分析SQL查询执行性能的重要工具的讨论。这个工具允许用户查看SQL语句的执行计划,从而理解数据库如何处理这些语句,以及哪些操作可能影响...

    高清彩版 SQL Server Execution Plans Second Edition

    《高清彩版 SQL Server Execution Plans 第二版》是Grant Fritchey所著的一本专注于SQL Server查询执行计划的书籍。本书的前身于2008年出版,而第二版则在2012年推出了新的内容,以帮助读者深入理解SQL Server的内部...

    Oracle Database 10g PL-SQL Programming

    ### Oracle Database 10g PL/SQL Programming #### 1. Introduction to PL/SQL PL/SQL (Procedural Language for SQL) is a procedural extension to SQL that provides a powerful way to manipulate data within...

Global site tag (gtag.js) - Google Analytics