How to get the SQL execution plan

Source: Internet
Author: User
Tags count hash sort

***********************************************************

----1: Get the execution plan for "just" display_cursor

***********************************************************

Explain the plan command in Oracle, you can directly parse subsequent SQL statements and save execution plans in a plan_table intermediate table. Then the method of Dbms_xplan package is obtained.

Select COUNT (*) from T1;

--Query The V$sql view to find the sql_id of the statement (provided that the SQL statement you are querying is still in shared pool):

Select sql_id from V$sql where sql_text= ' select COUNT (*) from T1 ';

sql_id

-------------

5bc0v4my7dvr5

--Call the Dbms_xplan package to see the implementation plan when the statement executes:

SELECT * FROM table (dbms_xplan.display_cursor (' 5BC0V4MY7DVR5 '));

Plan_table_output

--------------------------------------------------------------------------------

sql_id 5BC0V4MY7DVR5, child number 0

-------------------------------------

Select COUNT (*) from T1

Plan Hash value:3724264953

-------------------------------------------------------------------

| Id | Operation | Name | Rows | Cost (%CPU) | Time |

-------------------------------------------------------------------

| 0 |      SELECT STATEMENT |       |    |          74 (100) | |

|  1 |      SORT AGGREGATE |     |            1 |          | |

Plan_table_output

--------------------------------------------------------------------------------

|   2 | TABLE ACCESS full|   T1 |    100k| 74 (2) | 00:00:01 |

-------------------------------------------------------------------

Note

-----

-SQL Plan baseline sql_plan_f4251dfwsquh4616acf47 used for this statement

18 rows have been selected.

/*************

Call Display_cursor directly, and without specifying SQL_ID, you can query the SQL command execution plan just executed by the current session from the library cache.

Note: Display_cursor also supports the format parameter, which allows you to extract detailed execution plan information.

Can only be used on Sqlplus or SQLPLUSW. If you are Toad, pl/sql develop, and other tripartite tools, you may not be able to invoke normal use.

***********/

***********************************************************

2:explain Plan for

***********************************************************

---Case 1, show a simple Plan

Explain plan for SELECT COUNT (*) from T1;

View results:

SELECT * FROM table (Dbms_xplan.display ());

Plan_table_output

--------------------------------------------------------------------------------

Plan Hash value:3724264953

-------------------------------------------------------------------

| Id | Operation | Name | Rows | Cost (%CPU) | Time |

-------------------------------------------------------------------

| 0 |      SELECT STATEMENT |     |    1 | 74 (2) | 00:00:01 |

|  1 |      SORT AGGREGATE |     |            1 |          | |

|   2 | TABLE ACCESS full|   T1 |    100k| 74 (2) | 00:00:01 |

-------------------------------------------------------------------

Note

Plan_table_output

--------------------------------------------------------------------------------

-----

-SQL Plan Baseline "sql_plan_f4251dfwsquh4616acf47" used for this statement

13 rows have been selected.

--Case 2, show detailed execution plan information

Explain plan for SELECT COUNT (*) from T1;

View results:

SELECT * FROM table (Dbms_xplan.display (Null,null, ' advanced '));

Plan_table_output

--------------------------------------------------------------------------------

Plan Hash value:3724264953

-------------------------------------------------------------------

| Id | Operation | Name | Rows | Cost (%CPU) | Time |

-------------------------------------------------------------------

| 0 |      SELECT STATEMENT |     |    1 | 74 (2) | 00:00:01 |

|  1 |      SORT AGGREGATE |     |            1 |          | |

|   2 | TABLE ACCESS full|   T1 |    100k| 74 (2) | 00:00:01 |

-------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.