Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene...

33
Executing Explain Plans and Explaining Execution Plans Craig Martin 03/30/2012

Transcript of Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene...

Page 1: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Executing Explain PlansandExplaining Execution Plans

Craig Martin03/30/2012

Page 2: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Why?

Gene Kranz John Wooden

Page 3: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Why?

“Work the problem people. Don’t make things worse by guessing” - Gene Kranz

“If you don't have time to do it right, when will you have time to do it over?” - John Wooden

Page 4: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Explain Plan - Example

Page 5: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Displaying Results

● Query PLAN_TABLE Directly● Explain Plan feature in SQL Developer● DBMS_XPLAN package

Page 6: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DBMS_XPLAN.DISPLAY

● Pros● Actual SQL doesn’t have to be executed just

explained● Cons

● The plan produced may not be the actual plan used when executing the SQL

Page 7: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DBMS_XPLAN.DISPLAY_CURSOR

● Pros● Can display plan information for any SQL in cursor cache● Allows displaying of additional statistics (I/O, memory,

timing)● Cons

● SQL must have already been executed

Page 8: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DBMS_XPLAN.DISPLAY_CURSOR

● Parameters● SQL_ID● Child Number● Format

● Gathering Additional Details● Memory Management Statistics

● Set parameter pga_aggregate_target to non-zero● I/O Statistics

● Set parameter statistics_level to “ALL”● Use gather_plan_statistics hint during execution

Page 9: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DISPLAY_CURSOR: Output

● Up to 7 sections possible● Information about SQL Statement● Execution Plan● Query Blocks● Outline● Predicates● Column Projection● Notes

Page 10: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DISPLAY_CURSOR: Output

Information about SQL

Page 11: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DISPLAY_CURSOR: Output

Execution Plan

Page 12: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DISPLAY_CURSOR: Output

Query Blocks

Page 13: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DISPLAY_CURSOR: Output

Outline

Page 14: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DISPLAY_CURSOR: Output

Predicates

● Access = Only matching rows are retrieved● Filter = All rows are retrieved, matching rows kept

Page 15: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DISPLAY_CURSOR: Output

Column Projection

Page 16: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DISPLAY_CURSOR: Output

Notes

Page 17: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DISPLAY_CURSOR: Output

Page 18: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

DISPLAY_CURSOR: Output

Page 19: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Reading Execution Plans

● The only operation without a parent is the root node● A parent can have one or many children● A child can only have one parent● Children are displayed indented to the right of their

parent● All children of a single parent have the same

indentation● A parent is displayed before its children● The ID of a parent is less than the IDs of its children● If there are several nodes with the same indentation as

the parent, the node closest to the child is the parent

Page 20: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Reading Execution Plans

● From Oracle Documentation (11.2)● http://download.oracle.com/docs/cd/E11882_01/server.112/e16638/optimops.htm#i82029

Page 21: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

3

1

2

4

5

7

6

Page 22: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Common Operations

● Stand-Alone Operations● Most operations of this type● Parents have at most one child● Child executed at most once● Child feeds its parent● Examples:

● TABLE ACCESS● HASH GROUP BY● COUNT

Page 23: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Common Operations

● Unrelated-Combine Operations● Parents have multiple children that are executed

independently● Children are executed sequentially

● Starts with child with smallest ID● Every child executed at most once● Every child feeds its parent● Examples:

● HASH JOIN● MERGE JOIN● UNION-ALL

Page 24: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Common Operations

● Related-Combine Operations● Parents have multiple children where one child

controls execution of the other children● Children are not executed sequentially● Only first child executed at most once

● All others may be executed many times or not at all● Not every child feeds its parent

● Some are used only as restrictions● Examples:

● NESTED LOOPS● UPDATE● FILTER

Page 25: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

3

1

2

4

5

7

6

Page 26: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

7

1

2

3

4

6

5

Page 27: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”
Page 28: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

7

1

2 3

4

6

5 10

8 9

11 12

13

14 15

Page 29: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

7

1

3

4

6

5

10

8

9

11

12

13

14

15

2

16

18

20 21

22

23

19

17

Page 30: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

What to Ignore

● Cost!● Can’t be directly

controlled● May not be an accurate

indication of performance

● Rules of Always● Full table scans are

always bad● Indexes are always

good

Page 31: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

What to Look For

● Estimates that don’t match the actual data● Inaccurate statistics may exist

● Wasted Operations / Rows● Many more rows are read for an operation than

are used● Full scans● Unselective range scans● Wrong join order● Late filter operations

● High Execution Time / Buffer Count

Page 32: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

References

● Oracle Documentation● Oracle Database Performance Tuning Guide● Oracle Database PL/SQL Packages and Types

Reference● Troubleshooting Oracle Performance by

Christian Antognini

Page 33: Explaining Execution Plans and · Explaining Execution Plans Craig Martin 03/30/2012. Why? Gene Kranz John Wooden. Why? “Work the problem people. Don’t make things worse by guessing”

Questions?

http://[email protected]