1. 程式人生 > 實用技巧 >【Oracle Database】獲取sql語句的執行計劃

【Oracle Database】獲取sql語句的執行計劃

[oracle@wallet01 ~]$ sqlplus soe/soe
SQL> explain plan for
  2  SELECT
  3     c."NLS_TERRITORY" country, SUM (o.order_total) total
  4  FROM
  5     customers c
  6  JOIN orders o ON c.customer_id = o.customer_id
  7  GROUP BY
  8     c."NLS_TERRITORY"
  9  ORDER BY
 10     total DESC;

Explained.

SQL> select * from table(dbms_xplan.display());

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 867190458

------------------------------------------------------------------------------------------
| Id  | Operation            | Name      | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |           |    80 |  1920 |       | 12735   (1)| 00:02:33 |
|   1 |  SORT ORDER BY       |           |    80 |  1920 |       | 12735   (1)| 00:02:33 |
|   2 |   HASH GROUP BY      |           |    80 |  1920 |       | 12735   (1)| 00:02:33 |
|*  3 |    HASH JOIN         |           |  1429K|    32M|    25M| 12662   (1)| 00:02:32 |
|   4 |     TABLE ACCESS FULL| CUSTOMERS |  1000K|    14M|       |  4577   (1)| 00:00:55 |
|   5 |     TABLE ACCESS FULL| ORDERS    |  1429K|    12M|       |  5376   (1)| 00:01:05 |
------------------------------------------------------------------------------------------

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

   3 - access("C"."CUSTOMER_ID"="O"."CUSTOMER_ID")

17 rows selected.
[oracle@wallet01 ~]$ sqlplus / as sysdba
SQL> @$ORACLE_HOME/sqlplus/admin/plustrce.sql

SQL> grant plustrace to soe;
Grant succeeded.

SQL> conn soe/soea
Connected.

SQL> set autotrace traceonly
SQL> set line 200
SQL> SELECT
  2     c."NLS_TERRITORY" country, SUM (o.order_total) total
  3  FROM
  4     customers c
  5  JOIN orders o ON c.customer_id = o.customer_id
  6  GROUP BY
  7     c."NLS_TERRITORY"
  8  ORDER BY
  9     total DESC;

80 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 867190458

------------------------------------------------------------------------------------------
| Id  | Operation            | Name      | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |           |    80 |  1920 |       | 12735   (1)| 00:02:33 |
|   1 |  SORT ORDER BY       |           |    80 |  1920 |       | 12735   (1)| 00:02:33 |
|   2 |   HASH GROUP BY      |           |    80 |  1920 |       | 12735   (1)| 00:02:33 |
|*  3 |    HASH JOIN         |           |  1429K|    32M|    25M| 12662   (1)| 00:02:32 |
|   4 |     TABLE ACCESS FULL| CUSTOMERS |  1000K|    14M|       |  4577   (1)| 00:00:55 |
|   5 |     TABLE ACCESS FULL| ORDERS    |  1429K|    12M|       |  5376   (1)| 00:01:05 |
------------------------------------------------------------------------------------------

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

   3 - access("C"."CUSTOMER_ID"="O"."CUSTOMER_ID")

Statistics
----------------------------------------------------------
         22  recursive calls
          0  db block gets
      36663  consistent gets
      39279  physical reads
          0  redo size
       2944  bytes sent via SQL*Net to client
        578  bytes received via SQL*Net from client
          7  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
         80  rows processed
[oracle@wallet01 ~]$ sqlplus / as sysdba
SQL> grant select on v_$sql_plan to soe;
Grant succeeded.

SQL> grant select on v_$session to soe;
Grant succeeded.

SQL> grant select on v_$sql_plan_statistics_all to soe;
Grant succeeded.

SQL> grant select on v_$sql to soe;
Grant succeeded.

SQL> conn soe/soea
Connected.

SQL> alter session set statistics_level=all;
Session altered.	

SQL> set line 200
	 
SQL> SELECT
  2     c."NLS_TERRITORY" country, SUM (o.order_total) total
  3  FROM
  4     customers c
  5  JOIN orders o ON c.customer_id = o.customer_id
  6  GROUP BY
  7     c."NLS_TERRITORY"
  8  ORDER BY
  9     total DESC;
  
 SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  c7ws9qwcbqy3r, child number 2
-------------------------------------
SELECT  c."NLS_TERRITORY" country, SUM (o.order_total) total FROM
customers c JOIN orders o ON c.customer_id = o.customer_id GROUP BY
c."NLS_TERRITORY" ORDER BY  total DESC

Plan hash value: 867190458

---------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation            | Name      | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  | Writes |  OMem |  1Mem | Used-Mem | Used-Tmp|
---------------------------------------------------------------------------------------------------------------------------------------------------

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |           |      1 |        |     80 |00:00:04.56 |   36663 |  39279 |   2625 |       |       |          |         |
|   1 |  SORT ORDER BY       |           |      1 |     80 |     80 |00:00:04.56 |   36663 |  39279 |   2625 |  9216 |  9216 | 8192  (0)|         |
|   2 |   HASH GROUP BY      |           |      1 |     80 |     80 |00:00:04.56 |   36663 |  39279 |   2625 |   121M|    10M| 3869K (0)|         |
|*  3 |    HASH JOIN         |           |      1 |   1429K|   1429K|00:00:03.99 |   36663 |  39279 |   2625 |    53M|  8094K|   50M (1)|   22528 |
|   4 |     TABLE ACCESS FULL| CUSTOMERS |      1 |   1000K|   1000K|00:00:00.29 |   16865 |  16861 |      0 |       |       |          |         |
|   5 |     TABLE ACCESS FULL| ORDERS    |      1 |   1429K|   1429K|00:00:00.42 |   19798 |  19793 |      0 |       |       |          |         |
---------------------------------------------------------------------------------------------------------------------------------------------------

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

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   3 - access("C"."CUSTOMER_ID"="O"."CUSTOMER_ID")

24 rows selected.