1 / 22

Objectives

Objectives. After completing this lesson, you should be able to do the following: Use the EXPLAIN PLAN command to show how a statement is processed Use the DBMS_XPLAN package Use the Automatic Workload Repository Query the V$SQL_PLAN performance view

Download Presentation

Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objectives • After completing this lesson, you should be able to do the following: • Use the EXPLAINPLAN command to show how a statement is processed • Use the DBMS_XPLAN package • Use the Automatic Workload Repository • Query the V$SQL_PLAN performance view • Use the SQL*Plus AUTOTRACE setting to show SQL statement execution plans and statistics

  2. What Is an Execution Plan? • An execution plan is a set of steps that are performed by the optimizer in executing a SQL statement and performing an operation.

  3. Methods for Viewing Execution Plans • EXPLAIN PLAN • SQL Trace • Statspack • Automatic Workload Repository • V$SQL_PLAN • SQL*Plus AUTOTRACE

  4. Using Execution Plans • Determining the current execution plan • Identifying the effect of indexes • Determining access paths • Verifying the use of indexes • Verifying which execution plan may be used

  5. DBMS_XPLAN Package: Overview • The DBMS_XPLAN package provides an easy way to display the output from: • EXPLAIN PLAN command • Automatic Workload Repository (AWR) • V$SQL_PLAN and V$SQL_PLAN_STATISTICS_ALL fixed views • The DBMS_XPLAN package supplies three table functions that can be used to retrieve and display the execution plan: • DISPLAY • DISPLAY_CURSOR • DISPLAY_AWR

  6. EXPLAINPLAN Command • Generates an optimizer execution plan • Stores the plan in the PLAN table • Does not execute the statement itself

  7. EXPLAIN PLAN SET STATEMENT_ID= 'text' INTO your plan table EXPLAINPLAN Command FOR statement

  8. EXPLAINPLAN Command: Example EXPLAIN PLAN SET STATEMENT_ID = 'demo01' FOR SELECT e.last_name, d.department_name FROM hr.employees e, hr.departments d WHERE e.department_id = d.department_id; Explained. Note: The EXPLAIN PLAN command does not actually execute the statement.

  9. EXPLAINPLAN Command: Output SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY()); Plan hash value: 2933537672 ------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU| -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 106 | 2862 | 6 (17| | 1 | MERGE JOIN | | 106 | 2862 | 6 (17| | 2 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 27 | 432 | 2 (0| | 3 | INDEX FULL SCAN | DEPT_ID_PK | 27 | | 1 (0| |* 4 | SORT JOIN | | 107 | 1177 | 4 (25| | 5 | TABLE ACCESS FULL | EMPLOYEES | 107 | 1177 | 3 (0| -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID") filter("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID") 18 rows selected.

  10. Parse Tree SELECT STATEMENT 0 1 MERGE JOIN SORT JOIN 4 2 TABLE ACCESS BY INDEX ROWIDofDEPARTMENTS FULL TABLE SCANofEMPLOYEES 5 3 INDEX FULL SCAN DEPT_ID_PK

  11. Using the V$SQL_PLAN View • V$SQL_PLAN provides a way of examining the execution plan for cursors that were recently executed. • Information in V$SQL_PLAN is very similar to the output of an EXPLAIN PLAN statement: • EXPLAIN PLAN shows a theoretical plan that can be used if this statement were to be executed. • V$SQL_PLAN contains the actual plan used.

  12. HASH_VALUE ADDRESS CHILD_NUMBER POSITION PARENT_ID ID Hash value of the parent statement in the library cache Object number of the table or the index Child cursor number using this execution plan Order of processing for operations that all have the same PARENT_ID ID of the next execution step that operates on the output of the current step Number assigned to each step in the execution plan V$SQL_PLAN Columns • Note: This is only a partial listing of the columns.

  13. Querying V$SQL_PLAN SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('47ju6102uvq5q')); SQL_ID 47ju6102uvq5q, child number 0 ------------------------------------- SELECT e.last_name, d.department_name FROM hr.employees e, hr.departments d WHERE e.department_id =d.department_id Plan hash value: 2933537672 -------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU| -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 6 (100| | 1 | MERGE JOIN | | 106 | 2862 | 6 (17| | 2 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 27 | 432 | 2 (0| | 3 | INDEX FULL SCAN | DEPT_ID_PK | 27 | | 1 (0| |* 4 | SORT JOIN | | 107 | 1177 | 4 (25| | 5 | TABLE ACCESS FULL | EMPLOYEES | 107 | 1177 | 3 (0| -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID") filter("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID") 24 rows selected.

  14. V$SQL_PLAN_STATISTICS View • V$SQL_PLAN_STATISTICS provides actual execution statistics. • V$SQL_PLAN_STATISTICS_ALL enables side-by-side comparisons of the optimizer estimates.

  15. Automatic Workload Repository • Collects, processes, and maintains performance statistics for problem-detection and self-tuning purposes • Statistics include: • Object statistics • Time-model statistics • Some system and session statistics • Active Session History (ASH) statistics • Automatically generates snapshots of the performance data

  16. Managing AWR with PL/SQL • Creating snapshots • Dropping snapshots • Managing snapshot settings

  17. AWR Views • V$ACTIVE_SESSION_HISTORY • V$metric views • DBA_HIST views: • DBA_HIST_ACTIVE_SESS_HISTORY • DBA_HIST_BASELINE DBA_HIST_DATABASE_INSTANCE • DBA_HIST_SNAPSHOT • DBA_HIST_SQL_PLAN • DBA_HIST_WR_CONTROL

  18. Querying the AWR SELECT PLAN_TABLE_OUTPUT FROM TABLE (DBMS_XPLAN.DISPLAY_AWR('454rug2yva18w')); PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------------- SQL_ID 454rug2yva18w -------------------- select /* example */ * from hr.employees natural join hr.departments Plan hash value: 4179021502 ---------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 6 (100)| | | 1 | HASH JOIN | | 11 | 968 | 6 (17)| 00:00:01 | | 2 | TABLE ACCESS FULL| DEPARTMENTS | 11 | 220 | 2 (0)| 00:00:01 | | 2 | TABLE ACCESS FULL| DEPARTMENTS | 11 | 220 | 2 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| EMPLOYEES | 107 | 7276 | 3 (0)| 00:00:01 | ----------------------------------------------------------------------------------

  19. SQL*Plus AUTOTRACE OFF ON SET AUTOTRACE TRACE[ONLY] EXPLAIN STATISTICS SHOW AUTOTRACE

  20. SQL*Plus AUTOTRACE: Examples • To start tracing statements using AUTOTRACE • To hide statement output • To display execution plans only • Control the layout with column settings set autotrace on set autotrace traceonly set autotrace traceonly explain

  21. SQL*Plus AUTOTRACE: Statistics • set autotrace traceonly statistics • SELECT * • FROM products; Statistics ------------------------------------------------------ 1 recursive calls 0 db block gets 9 consistent gets 3 physical reads 0 redo size 15028 bytes sent via SQL*Net to client 556 bytes received via SQL*Net from client 6 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 72 rows processed

  22. Summary • In this lesson, you should have learned how to: • Use EXPLAINPLAN to view execution plans • Query V$SQL_PLAN to see the execution plan for cursors that were recently executed • Use the Automatic Workload Repository • Use SQL*Plus AUTOTRACE to run statementsand display execution plans and statistics

More Related