Home Database Oracle How to view Oracle execution plan

How to view Oracle execution plan

May 11, 2020 pm 05:29 PM
oracle

How to view Oracle execution plan

What is an execution plan?

SQL is a fool-like language. Each condition is a requirement. Different access orders form different execution plans. Oracle must make a choice and can only have one access path at a time. The execution plan is a description of the execution process or access path of a query statement in Oracle.

Selection of execution plan:

Usually a SQL has multiple execution plans, so how do we choose? The one with lower execution overhead means better performance and faster speed. We will choose which one. This process is called Oracle's parsing process, and then Oracle will put the better execution plan into the Shared Pool of SGA. To execute the same SQL later, you only need to obtain it from the Shared Pool, and there is no need to analyze it again.

Execution plan selection basis:

Select an execution plan based on statistical information.

Statistical information:

What is statistical information: number of records, number of blocks, etc., please view dba_tables / dba_indexes

Dynamic sampling:

Oracle normally collects statistical information at a certain time every day. How does Oracle collect statistical information for newly created tables? Use dynamic sampling.

set autotrace on
set linesize 1000
--Execute SQL statement
--The dynamic sampling used for this statement(level=2) key will appear

Six execution plans

Oracle provides 6 execution plan acquisition methods, each with different emphasis:

Generally follow the following rules when selecting:

1. If the sql execution takes a long time to produce results or cannot return results, use method 1: explain plan for

2. The easiest way to track a certain sql is method 1: explain plan for, followed by method 2: set autotrace on

3. If you want to check multiple execution plans of a certain sql, you can only use method 4: dbms_xplan.display_cursor or method 6: awrsqrpt.sql

4. If the sql contains a function, and the function contains sql, that is, there are multiple layers of calls. If you want to accurately analyze, you can only use method 5: 10046 tracking

5. Idea to see the real execution plan , you cannot use method 1: explain plan for and method 2: set autotrace on

6. If you want to get the number of times the table has been accessed, you can only use method 3: statistics_level = all

How to view Oracle execution plan

How Oracle collects statistics:

1. Oracle will choose to collect table and index statistics in a specific time period (default Monday to Friday: 22:00, Saturday and Sunday: 06:00), users can adjust by themselves, mainly to avoid peak periods;

2. There is a threshold limit for the analysis of tables and indexes, and analysis will only be performed automatically when the threshold is exceeded. . If the data changes are not large, Oracle will not analyze it;

3. The collection method is flexible. It can be done for a certain partition of the partition table, and a parallel mechanism can be used to collect table and index information;

How to collect statistical information:

--Collect table statistical information

1

2

exec dbms_stats.gather_table_stats(ownname => 'AAA', tabname => 'TEST02',estimate_percent =>

10,method_opt => 'for all indexed columns');

Copy after login

--Collect index statistics

1

2

exec dbms_stats.gather_index_stats(ownname => 'AAA',indname => 'ID_IDX',estimate_percent =>

10,degree => '4');

Copy after login

--Collect table and index statistics

1

2

exec dbms_stats.gather_table_stats(ownname => 'AAA',tabname => 'TEST02',estimate_percent =>

10,method_opt => 'for all indexed columns',cascade => true);

Copy after login

(1) explain plan for

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

SQL> show user

     USER 为 "HR"

SQL> set linesize 1000

SQL> set pagesize 2000

SQL> explain plan for

2 select *

3 from employees,jobs

4 where employees.job_id=jobs.job_id

5 and employees.department_id=50;

已解释。

  

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

  

PLAN_TABLE_OUTPUT

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

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

Plan hash value: 303035560

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

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

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

| 0 | SELECT STATEMENT | | 45 | 4590 | 6 (17)| 00:00:01 |

| 1 | MERGE JOIN | | 45 | 4590 | 6 (17)| 00:00:01 |

| 2 | TABLE ACCESS BY INDEX ROWID| JOBS | 19 | 627 | 2 (0)| 00:00:01 |

| 3 | INDEX FULL SCAN | JOB_ID_PK | 19 | | 1 (0)| 00:00:01 |

|* 4 | SORT JOIN | | 45 | 3105 | 4 (25)| 00:00:01 |

|* 5 | TABLE ACCESS FULL | EMPLOYEES | 45 | 3105 | 3 (0)| 00:00:01 |

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

Predicate Information (identified by operation id):

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

4 - access("EMPLOYEES"."JOB_ID"="JOBS"."JOB_ID")

filter("EMPLOYEES"."JOB_ID"="JOBS"."JOB_ID")

5 - filter("EMPLOYEES"."DEPARTMENT_ID"=50)

已选择19行。

Copy after login

Advantages: No need to actually execute, Fast and convenient;

Disadvantages:

1. No relevant statistical information is output, such as how many logical reads, how many physical reads, and how many recursive calls are generated;

2. It is impossible to determine how many rows have been processed;

3. It is impossible to determine how many times the table has been executed

(2) set autotrace on

Usage:

Command function:

SET AUTOT[RACE] OFF stops AutoTrace
SET AUTOT[RACE] ON turns on AutoTrace, displays AUTOTRACE information and SQL execution results
SET AUTOT[RACE] TRACEONLY turns on AutoTrace , only display AUTOTRACE information
SET AUTOT[RACE] ON EXPLAIN Turn on AutoTrace, display only AUTOTRACE EXPLAIN information
SET AUTOT[RACE] ON STATISTICS Turn on AutoTrace, display only AUTOTRACE STATISTICS information

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

SQL> set autotrace on

SQL> select * from employees,jobs where employees.job_id=jobs.job_id and employees.department_id=50;

--输出结果(略)

-- ...

已选择45行。

  

执行计划

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

Plan hash value: 303035560

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

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

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

| 0 | SELECT STATEMENT | | 45 | 4590 | 6 (17)| 00:00:01 |

| 1 | MERGE JOIN | | 45 | 4590 | 6 (17)| 00:00:01 |

| 2 | TABLE ACCESS BY INDEX ROWID| JOBS | 19 | 627 | 2 (0)| 00:00:01 |

| 3 | INDEX FULL SCAN | JOB_ID_PK | 19 | | 1 (0)| 00:00:01 |

|* 4 | SORT JOIN | | 45 | 3105 | 4 (25)| 00:00:01 |

|* 5 | TABLE ACCESS FULL | EMPLOYEES | 45 | 3105 | 3 (0)| 00:00:01 |

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

Predicate Information (identified by operation id):

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

4 - access("EMPLOYEES"."JOB_ID"="JOBS"."JOB_ID")

filter("EMPLOYEES"."JOB_ID"="JOBS"."JOB_ID")

5 - filter("EMPLOYEES"."DEPARTMENT_ID"=50)

统计信息

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

0 recursive calls

0 db block gets

13 consistent gets

0 physical reads

0 redo size

5040 bytes sent via SQL*Net to client

433 bytes received via SQL*Net from client

4 SQL*Net roundtrips to/from client

1 sorts (memory)

0 sorts (disk)

45 rows processed

Copy after login

Advantages:

1. Can output relevant statistical information at runtime (how many logical reads are generated, how many recursive calls, how many physical reads, etc.);

2. Although it has to wait The execution plan can only be output after the statement is executed, but the traceonly switch can be used to control the return results without printing the screen output;

Disadvantages:

1. You must wait for the SQL statement to be executed before the results are output;

2. It is impossible to see how many times the table has been accessed;

(3) statistics_level=all

Step 1: ALTER SESSION SET STATISTICS_LEVEL=ALL;

Step 2: Execute the SQL to be analyzed

Step 3: select * from table(dbms_xplan.display_cursor('sql_id/hash_value',null,'allstats last'));

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

SQL> alter session set statistics_level=all;

SQL> select * from employees,jobs where employees.job_id=jobs.job_id and employees.department_id=50;

--输出结果

--...

已选择45行。

  

SQL> set linesize 1000

SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));

  

PLAN_TABLE_OUTPUT

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

-----------

SQL_ID d8jzhcdwmd9ut, child number 0

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

select * from employees,jobs where employees.job_id=jobs.job_id and

employees.department_id=50

Plan hash value: 303035560

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

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

| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | OMem |

1Mem | Used-Mem |

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

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

| 0 | SELECT STATEMENT | | 1 | | 45 |00:00:00.01 | 13 | 8 | |

| |

PLAN_TABLE_OUTPUT

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

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

| 1 | MERGE JOIN | | 1 | 45 | 45 |00:00:00.01 | 13 | 8 | |

| |

| 2 | TABLE ACCESS BY INDEX ROWID| JOBS | 1 | 19 | 19 |00:00:00.01 | 6 | 2 | |

| |

| 3 | INDEX FULL SCAN | JOB_ID_PK | 1 | 19 | 19 |00:00:00.01 | 3 | 1 | |

| |

|* 4 | SORT JOIN | | 19 | 45 | 45 |00:00:00.01 | 7 | 6 | 6144 |

6144 | 6144 (0)|

|* 5 | TABLE ACCESS FULL | EMPLOYEES | 1 | 45 | 45 |00:00:00.01 | 7 | 6 | |

| |

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

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

Predicate Information (identified by operation id):

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

4 - access("EMPLOYEES"."JOB_ID"="JOBS"."JOB_ID")

PLAN_TABLE_OUTPUT

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

-----

filter("EMPLOYEES"."JOB_ID"="JOBS"."JOB_ID")

5 - filter("EMPLOYEES"."DEPARTMENT_ID"=50)

已选择25行。

Copy after login

Keyword interpretation:

1, starts: the number of SQL executions;

2, E-Rows: the number of rows expected to be returned by the execution plan;

3, R-Rows : The number of rows actually returned by the execution plan;

4. A-Time: The execution time of each step (HH:MM:SS.FF). Based on this row, you can know where the SQL time is spent;

5. Buffers: logical read or consistent read actually executed in each step;

6. Reads: physical read;

Advantages:

1. Can be clear Get how many times the table has been accessed from starts;

2、可以从E-Rows和A-Rows得到预测的行数和真实的行数,从而可以准确判断Oracle评估是否准确;

3、虽然没有准确的输出运行时的相关统计信息,但是执行计划中的Buffers就是真实的逻辑读的数值;

缺点:

1、必须要等执行完后才能输出结果;

2、无法控制结果打屏输出,不像autotrace可以设置traceonly保证不输出结果;

3、看不出递归调用,看不出物理读的数值

(4)dbms_xplan.display_cursor获取

步骤1:select * from table( dbms_xplan.display_cursor('&sql_id') ); --该方法是从共享池得到

注释:

1、还有1种方法,select * from table( dbms_xplan.display_awr('&sql_id') ); --该方法是从awr性能视图里面获取

2、如果有多个执行计划,可用以下方法查出:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

select * from table(dbms_xplan.display_cursor('&sql_id',0));

select * from table(dbms_xplan.display_cursor('&sql_id',1));

*/

SQL> select * from table(dbms_xplan.display_cursor('5hkd01f03y43d'));

PLAN_TABLE_OUTPUT

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

SQL_ID 5hkd01f03y43d, child number 0

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

select * from test where table_name = 'LOG$'

Plan hash value: 2408911181

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

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

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

| 0 | SELECT STATEMENT | | | | 2 (100)|

| 1 | TABLE ACCESS BY INDEX ROWID| TEST | 1 | 241 | 2 (0)|

|* 2 | INDEX RANGE SCAN | IDX_TEST_1 | 1 | | 1 (0)|

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

Predicate Information (identified by operation id):

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

2 - access("TABLE_NAME"='LOG$')

19 rows selected

Copy after login

注释:如何查看1个sql语句的sql_id,可直接查看v$sql

优点:

1、知道sql_id即可得到执行计划,与explain plan for一样无需执行;

2、可得到真实的执行计划

缺点:

1、没有输出运行的统计相关信息;

2、无法判断处理了多少行;

3、无法判断表被访问了多少次;

(5)事件10046 trace跟踪

步骤1:alter session set events '10046 trace name context forever,level 12'; --开启追踪

步骤2:执行sql语句;

步骤3:alter session set events '10046 trace name context off'; --关闭追踪

步骤4:找到跟踪后产生的文件(开启10046前先用‘ls -lrt’看一下文件,执行结束后再看哪个是多出来的文件即可)

步骤5:tkprof trc文件 目标文件 sys=no sort=prsela,exeela,fchela --格式化命令

优点:

1、可以看出sql语句对应的等待事件;

2、如果函数中有sql调用,函数中有包含sql,将会被列出,无处遁形;

3、可以方便的看处理的行数,产生的逻辑物理读;

4、可以方便的看解析时间和执行时间;

5、可以跟踪整个程序包

缺点:

1、步骤繁琐;

2、无法判断表被访问了多少次;

3、执行计划中的谓词部分不能清晰的展现出来

推荐:Oracle数据库学习教程

The above is the detailed content of How to view Oracle execution plan. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check tablespace size of oracle How to check tablespace size of oracle Apr 11, 2025 pm 08:15 PM

To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

How to encrypt oracle view How to encrypt oracle view Apr 11, 2025 pm 08:30 PM

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

How to view instance name of oracle How to view instance name of oracle Apr 11, 2025 pm 08:18 PM

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

How to uninstall Oracle installation failed How to uninstall Oracle installation failed Apr 11, 2025 pm 08:24 PM

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

How to import oracle database How to import oracle database Apr 11, 2025 pm 08:06 PM

Data import method: 1. Use the SQLLoader utility: prepare data files, create control files, and run SQLLoader; 2. Use the IMP/EXP tool: export data, import data. Tip: 1. Recommended SQL*Loader for big data sets; 2. The target table should exist and the column definition matches; 3. After importing, data integrity needs to be verified.

How to get time in oracle How to get time in oracle Apr 11, 2025 pm 08:09 PM

There are the following methods to get time in Oracle: CURRENT_TIMESTAMP: Returns the current system time, accurate to seconds. SYSTIMESTAMP: More accurate than CURRENT_TIMESTAMP, to nanoseconds. SYSDATE: Returns the current system date, excluding the time part. TO_CHAR(SYSDATE, 'YYY-MM-DD HH24:MI:SS'): Converts the current system date and time to a specific format. EXTRACT: Extracts a specific part from a time value, such as a year, month, or hour.

How to set up users of oracle How to set up users of oracle Apr 11, 2025 pm 08:21 PM

To create a user in Oracle, follow these steps: Create a new user using the CREATE USER statement. Grant the necessary permissions using the GRANT statement. Optional: Use the RESOURCE statement to set the quota. Configure other options such as default roles and temporary tablespaces.

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

See all articles