Oracle - How to Read a Query Plan
Contents
Goal: know how to generate and read a basic query plan.
Understand key phrases, such as Full Table Scan and Nested Loops should jump out as red flags. Be able to generate & analyze a SQL query plan
When you fire an SQL query to Oracle, Oracle database internally creates a query execution plan in order to fetch the desired data from the physical tables. The query execution plan is nothing but a set of methods on how the database will access the data from the tables. This query execution plan is crucial as different execution plans will need different cost and time for the query execution.
How the Execution Plan is created actually depends on what type of query optimizer is being used in your Oracle database. There are two different optimizer options – Rule based (RBO) and Cost based (CBO) Optimizer. For Oracle 10g, CBO is the default optimizer. Cost Based optimizer enforces Oracle to generate the optimization plan by taking all the related table statistics into consideration. On the other hand, RBO uses a fixed set of pre-defined rules to generate the query plan. Obviously such fixed set of rules may not always be able to create the plan that is most efficient in nature. This is because an efficient plan will depend heavily on the nature and volume of table’s data. Because of this reason, CBO is preferred over RBO.
How to read excution plan - basic
1 | SELECT PLAN_TABLE_OUTPUT |
See the below example, read detail execution four scan way:
1 | -- create simple table - product |
Full Table Scan
- Whole table is read upto high water mark
- Uses multiblock input/output
- Buffer from FTS operation is stored in LRU end of buffer cache
1 | -- explain plan |
Index Unique Scan
- Single block input/output
1 | -- create index for test |
Index Fast Full Scan
- Multi block i/o possible
- Returned rows may not be in sorted order
1 | -- explain plan |
Index Full Scan
- Single block i/o
- Returned rows generally will be in sorted order
Resources:
https://dwbi.org/database/oracle/38-oracle-query-plan-a-10-minutes-guide
https://dwbi.org/database/oracle/39-query-execution-plan-part2.html
Author: itabas016
Link: https://tech.itabas.com/2016/10/13/database/oracle-kt-how-to-read-a-query-plan/
License: CC BY-NC-ND 4.0