Professional Documents
Culture Documents
Explain Plan:
How to Read and Analyze Execution plans
Maria Colgan
Senior Principal Product Manager
Agenda
3
What is an Execution plan?
4
What is an Execution plan?
Query:
SELECT prod_category, avg(amount_sold)
FROM sales s, products p
WHERE p.prod_id = s.prod_id
GROUP BY prod_category;
5
How to get an Execution Plan
6
How to get an Execution Plan Example 1
7
How to get an Execution Plan Example 2
8
Generating a more detailed plan Add hint to gather run
time statistics to
compare against
Optimizer estimations
Compare the estimated number of rows returned for each operation in the
plan to actual rows returned
9
Agenda
10
Instructions for running the lab
11
Environment
12 12
Environment – What you should see on VirtualBox
The Hands-on
directory
Terminal
emacs
Local Oracle
documentation
13
Environment What you should see on VirtualBox
The Hands-on
directory
Terminal
emacs
Local Oracle
documentation
14
Starting the hands-on lab
15
Running the hands-on lab
16
Agenda
17
Exercise 1
Incorrect Access Path
• Scenario
– Optimizer underestimates cardinality of a simple query by 10X and
picks an index scan followed by a table access rather than a full
table scan
• Goal
– Determine why the Optimizer got the cardinality wrong and fix it
– No Optimizer hints are required
– You can’t delete or hide the index
• Getting Started
– Scripts are located in /hol/optimizer/exercise1
– To solve it on your own run advanced.sql You have
15 minutes
– To follow the worked example run intermediate.sql
18
Exercise 1
Incorrect Access Path
19
Exercise 1 Solution
Do you have accurate statistics?
20
Exercise 1 Solution
Do you have a data skew?
• What does the data look like in the big emp table?
Select deptno, count(*) from bigemp group by deptno;
There is a massive
data skew with 10
being the most
popular value in the
table
21
Exercise 1 Solution
Data skew?
22
Exercise 1 solution
What is a histogram?
23
Exercise 1 Solution
Create a histogram on deptno column
24
Agenda
25
Exercise 2
Incorrect cardinality estimate
• Scenario
– The optimizer is underestimating the rows returned by a simple
count(*) from the emp2 table
• Goal
– Determine why the optimizer got the cardinality wrong on emp2
and fix it
– No Optimizer hints are required
– No additional access structures are allowed
• Getting Started
– Scripts are located in /hol/optimizer/exercise2
– To solve it on your own run advanced.sql You have
15 mins
– To follow the worked example run intermediate.sql
26
Exercise 2
Incorrect cardinality estimate2
• Original Plan
27
Exercise 2 Solution
Do you have accurate statistics?
• Do we have statistics for the emp2 table?
Select table_name, num_distinct, histograms
From user_tab_col_statistics where table_name=‘EMP2’;
28
Exercise 2 Solution
Optimizer and Function wrapped indexes
29
Exercise 2 Solution
30
Agenda
31
Exercise 3
Join Order
• Scenario
– One of your end-users is complaining a query is slower after you
upgraded. Problem seems to be the join order for the query
• Goal
– Determine why the optimizer got the join order wrong and fix it
– No Optimizer hints are required
– No additional access structures allowed
• Getting Started
– Scripts are located in /hol/optimizer/exercise3
– To solve it on your own run advanced.sql You have
15 mins
– To follow the worked example run intermediate.sql
32
Exercise 3
Join Order
• The Query : SELECT c.cust_id, c.cust_city, s.amount_sold
FROM sh.sales, s,
scott.customers c,
sh.products p,
sh.promotions po,
WHERE s.cust_id = c.cust_id
AND s.prod_id = p.prod_id
AND s.promo_id = po.promo_id
AND c.country_id= ‘US’
AND c.cust_state_province = ‘CA’
AND p.prod_name=‘O/S Documentation Set – English’
AND po.promo_name=‘internet promotion #29-350’;
• Current join order: p -> c -> s -> po
• Desired join order: p -> po -> c-> s
33
Exercise 3
34
Exercise 3 Solution
Do you have accurate statistics?
35
Exercise 3 Solution
Take a closer look at the plan
37
Exercise 3 Solution
Correlated Columns
• Need some additional information to complete the formula
• Select count(*) from customers;
Cardinalityis #ROWS * 1 * 1
NDV c1 NDV c2
630 * 1/19 * 1/120 = 0.2 Optimizer rounds up to 1
38
Exercise 3 Solution
Create extended stats on the column group
39
Exercise 3 Solution
Create extended stats on the column group
40
Exercise 3 Solution
2
1
3
4
41
42