EXPLAIN PLAN   «Prev 

Detecting full-table scans using EXPLAIN PLAN

  1. With the Oracle Enterprise Manager open, click the Oracle SQL Analyzer button.
  2. Read the opening screen, and click OK.
  3. Now we are ready to begin. Select the bottommost database under the Databases folder.
  4. Select Top SQL from the mon1 database.
  5. The TopSQL Options box will allow you to control the number of disk reads as well as apply various filters. Click OK.
  6. SQL disk reads appear in ascending order. The first row, which begins select/*+first_rows*/ has already been selected for you. Click the SQL Text tab.
  7. The SQL text now appears in the SQL Text Tab window. Place your cursor anywhere in the SQL text field to highlight the text.
  8. In an actual Oracle environment, you would press Ctrl+C to copy this text and Ctrl+N to move to a new screen. For the purposes of this simulation, press Enter to move to a new SQL screen.
  9. For the purposes of this simulation, place your cursor in the SQL statement window and hit Enter to paste your SQL text. You would normally accomplish this by pressing Ctrl+V.
  10. Typically, you would press F9 to see the rule-based EXPLAIN PLAN, but for the purposes of this simulation press Enter.
  11. Here we see a full-table scan.


Viewing the Execution Plan

Execution plans are hierarchical in nature. That is, the task of executing a query is one large step, which can be broken down into one or more smaller steps. Each of those steps might then be broken down further into even smaller steps. This division continues until the bottom is reached. To look at such an execution plan, you need to execute a hierarchical query such as the one shown here:

SELECT LPAD(' ', 2*(level-1)) ||
  operation || ' ' || options
  || ' ' || object_name || ' ' ||
  DECODE(id, 0, 'Cost = ' || position)
  "Query Plan"
FROM plan_table
START WITH id = 0 AND statement_id = 'statement_id'
CONNECT BY PRIOR id = parent_id
  AND statement_id = 'statement_id';

The START WITH and CONNECT BY clauses in this query are used to organize the results in a hierarchy. The ID and PARENT_ID fields in the plan table determine that hierarchy and each step is indented underneath its parent. The LPAD expression in the query's select list is used to indent the results to reflect the hierarchy. Each plan table record contains an ID and a PARENT_ID column. The results of each step are fed as input into the parent step, which is identified by the PARENT_ID. If this query is used to view the plan for the emp_report statement that was explained in the previous section, the following results will be displayed:

Query Plan
-------------------------------------
SELECT STATEMENT Cost = 9
 SORT ORDER BY
  HASH JOIN
   MERGE JOIN CARTESIAN
    TABLE ACCESS FULL DEPT
     SORT JOIN 
      TABLE ACCESS FULL SALGRADE
       TABLE ACCESS FULL EMP

The step that is indented the most is executed first. In this example, the first step is a full table scan of the salgrade table. The results from this step are then fed as input into the parent step.