Since Oracle 10g, there is a nice BUILT-IN package named DBMS_XPLAN that gives a very nicely formatted explain plan. This blog article is just to show that you can still continue using the ‘set autotrace on’ SQL command in order to get your explain plan but you need to be aware that this SQL command use the estimated part of the DBMS_XPLAN package. In other words when you use autotrace Oracle is using behind the scene the explain plan for plus select * from table (dbms_xplan. display).
Let’s see this in action:
mhouri> cl scr mhouri> desc t Name Null? Type ----------------------- -------- ---------------- ID VARCHAR2(10) NAME VARCHAR2(100) mhouri> set linesize 150 mhouri> var x number mhouri> exec : x :=99999 PL/SQL procedure successfully completed. mhouri> explain plan for 2 select sum(length(name)) from t where id > : x; Explained. mhouri> select * from table(dbms_xplan.display); PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------- Plan hash value: 1188118800 ------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 23 | 4 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 23 | | | | 2 | TABLE ACCESS BY INDEX ROWID| T | 58 | 1334 | 4 (0)| 00:00:01 | |* 3 | INDEX RANGE SCAN | I | 11 | | 2 (0)| 00:00:01 | ------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("ID">:X) 15 rows selected. mhouri> set autotrace on mhouri> select sum(length(name)) from t where id > : x; SUM(LENGTH(NAME)) ----------------- 10146 Execution Plan ---------------------------------------------------------- Plan hash value: 1188118800 ------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 23 | 4 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 23 | | | | 2 | TABLE ACCESS BY INDEX ROWID| T | 58 | 1334 | 4 (0)| 00:00:01 | |* 3 | INDEX RANGE SCAN | I | 11 | | 2 (0)| 00:00:01 | ------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("ID">:X) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 15 consistent gets 0 physical reads 0 redo size 232 bytes sent via SQL*Net to client 243 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed mhouri> set autotrace off mhouri> select sum(length(name)) from t where id > : x; SUM(LENGTH(NAME)) ----------------- 10146 mhouri> select * from table(dbms_xplan.display_cursor); PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------------- SQL_ID 7zm570j6kj597, child number 0 ------------------------------------- select sum(length(name)) from t where id > : x Plan hash value: 1842905362 --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 5 (100)| | | 1 | SORT AGGREGATE | | 1 | 23 | | | |* 2 | TABLE ACCESS FULL| T | 59 | 1357 | 5 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter(TO_NUMBER("ID")>:X) 19 rows selected.