Wednesday, March 21, 2012

Explain Plan Using DBMS_XPLAN

Usage of DBMS_XPLAN:

explain plan set statement_id = 'skm' for
SELECT acct.account_number
     , acct.status Account_Status
     , acct.account_established_date
     , acct.account_termination_date
     , acct.account_activation_date
     , asite.bill_to_flag
     , asite.ship_to_flag
     , asite.customer_category_code
     , psite.party_site_name
     , psite.status Party_Site_Status
     , psite.start_date_active
     , psite.end_date_active
     , loc.address1 || ' ' || loc.address2 address
     , loc.city
     , loc.state
     , loc.postal_code
     , siteu.site_use_code
     , siteu.primary_flag
     , siteu.status site_uses_status
     , banku.start_date
     , banku.end_date 
     , banku.primary_flag
     , bank.bank_account_num
     , bank.bank_account_name
     , bank.inactive_date
  FROM apps.hz_cust_accounts         acct
     , apps.hz_cust_acct_sites_all   asite
     , apps.hz_party_sites           psite
     , apps.hz_locations             loc
     , apps.hz_cust_site_uses_all    siteu
     , apps.ap_bank_account_uses_all banku
     , apps.ap_bank_accounts_all     bank
 WHERE acct.account_number        = :account_number -- in single quotes
   AND asite.cust_account_id      = acct.cust_account_id
   AND asite.party_site_id        = psite.party_site_id
   AND psite.location_id          = loc.location_id
   AND siteu.cust_acct_site_id    = asite.cust_acct_site_id
   AND banku.customer_site_use_id = siteu.site_use_id
   AND bank.bank_account_id       = banku.external_bank_account_id
 ORDER BY bank.bank_account_num
/
Now run following sql statement

set pages 1000
set lines 132
select * from table(dbms_xplan.display('plan_table','skm'));

And you will see something like
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Plan hash value: 3845713369

----------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name                       | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                 |                            |  3264 |   299K| 38586   (1)| 00:07:44 |
|*  1 |  FILTER                          |                            |       |       |            |          |
|   2 |   NESTED LOOPS                   |                            |       |       |            |          |
|   3 |    NESTED LOOPS                  |                            |  3264 |   299K| 38586   (1)| 00:07:44 |
|   4 |     NESTED LOOPS                 |                            |  3264 |   248K| 28788   (1)| 00:05:46 |
|   5 |      NESTED LOOPS                |                            |  3585 |   213K| 18026   (1)| 00:03:37 |
|*  6 |       TABLE ACCESS FULL          | AP_SUPPLIERS               |  2534 |   113K|  9067   (2)| 00:01:49 |
|*  7 |       TABLE ACCESS BY INDEX ROWID| AP_SUPPLIER_SITES_ALL      |     1 |    15 |     4   (0)| 0
|*  8 |        INDEX RANGE SCAN          | AP_SUPPLIER_SITES_U2       |     1 |       |     2   (0)| 00:00:01
|*  9 |      TABLE ACCESS BY INDEX ROWID | IBY_EXTERNAL_PAYEES_ALL    |     1 |    17 |     3   (0)| 0
|* 10 |       INDEX RANGE SCAN           | IBY_EXTERNAL_PAYEES_ALL_N7 |     1 |       |     2   (0)| 00:00:01 
|* 11 |     INDEX RANGE SCAN             | IBY_EXT_PARTY_PMT_MTHDS_N1 |     1 |       |     2   (0)| 00:00:01 |
|* 12 |    TABLE ACCESS BY INDEX ROWID   | IBY_EXT_PARTY_PMT_MTHDS    |     1 |    16 |     3   (0)| 00
----------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(SYSDATE@!-300<=SYSDATE@!-290)
   6 - filter("SUP"."END_DATE_ACTIVE" IS NULL AND "SUP"."CREATION_DATE">=SYSDATE@!-300 AND
              "SUP"."CREATION_DATE"<=SYSDATE@!-290)
   7 - filter("SS"."INACTIVE_DATE" IS NULL)
   8 - access("SUP"."VENDOR_ID"="SS"."VENDOR_ID")
   9 - filter("EPA"."INACTIVE_DATE" IS NULL)
  10 - access("SS"."VENDOR_SITE_ID"="EPA"."SUPPLIER_SITE_ID")
       filter("EPA"."SUPPLIER_SITE_ID" IS NOT NULL)
  11 - access("EPA"."EXT_PAYEE_ID"="PPM"."EXT_PMT_PARTY_ID")
  12 - filter("PPM"."INACTIVE_DATE" IS NULL)

33 rows selected.
For Better looking output, you may run following sql statement

set pages 1000
set lines 132
spool plan.htm
select  dbms_xplan.display_plan( table_name    =>'plan_table'
                                      , statement_id  => 'skm'
                                      , format        => 'ALL'
                                      , filter_preds  => NULL
                                      , type          => 'HTML'
                                      )
 from dual
/
spool off
Now open plan.htm in a browser, you will a better layout of query plan.

No comments:

Post a Comment