Академический Документы
Профессиональный Документы
Культура Документы
Todays Session
EXPLAIN PLAN overview
TKPROF overview
Why???
Reading execution plans
Reading TKPROF reports
White Paper
Twenty one pages of details I can't possibly
cover in a one hour presentation!
Lots of sample code, execution plans, and
TKPROF reports that you will see are probably
not readable when I put them up on PowerPoint
slidesbut they are readable in the white paper.
Download: www.dbspecialists.com/presentations
Important Columns
in the Plan Table
statement_id Unique identifier for each execution plan
timestamp
When the execution plan was generated
operation
The operation performed in one step of the execution
plan, such as table access
optionsAdditional information about the operation, such as by
index ROWID
object_name Name of table, index, view, etc. accessed
optimizer
Optimizer goal used when creating execution plan
id Step number in execution plan
parent_id
Step number of parent step
EXPLAIN PLAN
Prerequisites
INSERT privilege on a plan table
All necessary privileges to execute the
statement being explained
SELECT privileges on underlying tables of
views, if the statement being explained
involves views
Querying an Execution
Plan from the Plan Table
Use a CONNECT BY clause to trace the hierarchy
Use LPAD function to indent rows, making the
hierarchy easier to follow
Put statement_id in WHERE clause to retrieve only
one execution plan at a time
Sample script on next slide shows the most
important information
You can also try utlxpls.sql or utlxplp.sql in
$ORACLE_HOME/rdbms/admin
9
10
11
Other Ways to
View Execution Plans
The autotrace feature in SQL*Plus
SET AUTOTRACE OFF|ON|TRACEONLY [EXPLAIN] [STATISTICS]
Sample Autotrace
Output in SQL*Plus
Execution Plan
---------------------------------------------------------0
SELECT STATEMENT Optimizer=CHOOSE (Cost=4 Card=1 Bytes=39)
1
0
NESTED LOOPS (Cost=4 Card=1 Bytes=39)
2
1
NESTED LOOPS (Cost=3 Card=1 Bytes=27)
3
2
TABLE ACCESS (BY INDEX ROWID) OF 'INVOICE_ITEMS' (Cost
=2 Card=1 Bytes=15)
4
3
INDEX (UNIQUE SCAN) OF 'INVOICE_ITEMS_PK' (UNIQUE) (
Cost=1 Card=2)
5
2
TABLE ACCESS (BY INDEX ROWID) OF 'INVOICES' (Cost=1 Ca
rd=2 Bytes=24)
6
5
INDEX (UNIQUE SCAN) OF 'INVOICES_PK' (UNIQUE)
7
1
TABLE ACCESS (BY INDEX ROWID) OF 'CUSTOMERS' (Cost=1 Car
d=100 Bytes=1200)
8
7
INDEX (UNIQUE SCAN) OF 'CUSTOMERS_PK' (UNIQUE)
13
14
EXPLAIN PLAN
Limitations
The EXPLAIN PLAN statement provides a good faith
estimate of the execution plan that Oracle would use.
The real plan that gets used may differ from what
EXPLAIN PLAN tells you for many reasons:
Optimizer stats, cursor sharing,, bind variable peeking,
dynamic instance parameters make plans less stable.
EXPLAIN PLAN does not peek at bind variables.
EXPLAIN PLAN does not check the library cache to see if
the statement has already been parsed.
16
18
In another session:
SYS.dbms_system.set_sql_trace_in_session
(<SID>, <serial#>, TRUE)
19
21
TKPROF Command-line
Arguments
tkprof <trace file> <output file> \
[explain=<username/password>] \
[sys=n] [sort=<keyword>]
trace file
output file
explain=
sys=n
sort=
cpu
elapsed
disk
query
current
-------- ---------- ---------- ---------- ---------0.05
0.02
0
0
0
0.00
0.00
0
0
0
0.00
0.00
8
8
0
-------- ---------- ---------- ---------- ---------0.05
0.02
8
8
0
rows
---------0
0
1
---------1
23
customer_id, customer_name
customers
UPPER (customer_name) LIKE 'ACME%'
customer_name;
OPERATION
OBJECT_NAME
------------------------------ -------------SELECT STATEMENT
SORT ORDER BY
TABLE ACCESS FULL
CUSTOMERS
26
Execution Plan
Operations
TABLE ACCESS FULL
Perform a full table scan of the indicated table and retrieve
all rows that meet criteria from the WHERE clause. Input:
no subordinate operations. Output: the necessary columns
from the rows meeting all criteria.
SORT ORDER BY
Sort the input rows for the purpose of satisfying an ORDER
BY clause. Input: the rows to be sorted. Output: the rows in
sorted order.
27
a.customer_name, b.invoice_number,
b.invoice_date
customers a, invoices b
b.invoice_date > TRUNC (SYSDATE - 1)
a.customer_id = b.customer_id;
OPERATION
-----------------------------SELECT STATEMENT
NESTED LOOPS
TABLE ACCESS BY INDEX ROWID
INDEX RANGE SCAN
TABLE ACCESS BY INDEX ROWID
INDEX UNIQUE SCAN
OBJECT_NAME
-------------INVOICES
INVOICES_DATE
CUSTOMERS
CUSTOMERS_PK
28
Execution Plan
Operations
INDEX UNIQUE SCAN
Look up a complete key in a unique index. Input: usually
no subordinate operations. (Key values typically come
from the original query or a parent operation.) Output:
zero or one ROWIDs from the index.
INDEX RANGE SCAN
Look up a key in a non-unique index, or an incomplete
key in a unique index. Input: usually no subordinate
operations. Output: zero or more ROWIDs from the index.
29
Execution Plan
Operations
TABLE ACCESS BY INDEX ROWID
Look up rows in a table by their ROWIDs. Input: a list of
ROWIDs to look up. Output: the necessary columns
from the rows with the given ROWIDs.
NESTED LOOPS
Perform a join between two sets of row data using the
nested loops algorithm. Inputs: two separate sets of row
data. Output: the results of the join.
For each row Oracle reads from the first input, the
operations that make up the second input are executed
once and matching rows generate output.
30
a.customer_name,
COUNT (DISTINCT b.invoice_id) open_invs,
COUNT (c.invoice_id) open_inv_items
FROM
customers a, invoices b, invoice_items c
WHERE
b.invoice_status = 'OPEN'
AND
a.customer_id = b.customer_id
AND
c.invoice_id (+) = b.invoice_id
GROUP BY a.customer_name;
OPERATION
-------------------------------SELECT STATEMENT
SORT GROUP BY
NESTED LOOPS OUTER
HASH JOIN
TABLE ACCESS BY INDEX ROWID
INDEX RANGE SCAN
TABLE ACCESS FULL
INDEX RANGE SCAN
OBJECT_NAME
----------------
INVOICES
INVOICES_STATUS
CUSTOMERS
INVOICE_ITEMS_PK
31
Execution Plan
Operations
HASH JOIN
Perform a join between two sets of row data using
the hash join algorithm. Inputs: two separate sets of
row data. Output: the results of the join.
Oracle reads all rows from the second input and
builds a hash structure, before reading each row
from the first input one at a time. For each row from
the first input, the hash structure is probed and
matching rows generate output.
32
Execution Plan
Operations
NESTED LOOPS OUTER
Same as the NESTED LOOPS operation, except
that an outer join is performed.
SORT GROUP BY
Same as the SORT ORDER BY operation, except
that the rows are sorted and grouped to satisfy a
GROUP BY clause.
33
customer_name
customers a
EXISTS
(SELECT 1
FROM
invoices_view b
WHERE b.customer_id = a.customer_id
AND
number_of_lines > 100)
ORDER BY customer_name;
CREATE OR REPLACE VIEW invoices_view AS
SELECT
a.invoice_id, a.customer_id,
COUNT(*) number_of_lines
FROM
invoices a, invoice_items b
WHERE
b.invoice_id = a.invoice_id
GROUP BY a.invoice_id, a.customer_id;
34
-------------
CUSTOMERS
INVOICES_VIEW
INVOICES
INVS_CUST_ID
INV_ITEMS_PK
35
Execution Plan
Operations
FILTER
Read a set of row data and discard some rows based
on various criteria. To determine the criteria,
operations from a second input may need to be
performed. Input: rows to be examined and,
sometimes, an additional subordinate operation that
must be performed for each row from the first input in
order to evaluate criteria. Output: the rows from the
first input that met the criteria.
36
Execution Plan
Operations
VIEW
Build a physical representation of a database
view or subset of a database view. Input: set of
row data. Output: set of row data that
implements the view or subset of the view.
37
customers a, contacts@sales.acme.com b
UPPER(b.contact_name) = UPPER(a.cust_name);
Execution Plan
-----------------------------------------------0
SELECT STATEMENT Optimizer=HINT: RULE
1
0 MERGE JOIN
2
1
SORT (JOIN)
3
2
REMOTE*
SALES.ACME.COM
4
1
SORT (JOIN)
5
4
TABLE ACCESS (FULL) OF 'CUSTOMERS'
3 SERIAL_FROM_REMOTE
SELECT "CONTACT_NAME"
FROM "CONTACTS" "B
39
Execution Plan
Operations
REMOTE
Submit a SQL statement to a remote database via Oracle
Net. Input: typically no subordinate operations. Output:
the results of the query from the remote database. Note
that the database link used to access the remote
database and the actual SQL submitted to the remote
database will be accessible from the execution plan.
40
Execution Plan
Operations
SORT JOIN
Same as the SORT GROUP BY operation, except that
the input is sorted by the join column or columns in
preparation for a join using the merge join algorithm.
41
Execution Plan
Operations
MERGE JOIN
Perform a join between two sets of row data using
the merge join algorithm. Inputs: two separate sets
of row data. Output: the results of the join.
Oracle reads rows from both inputs in an
alternating fashion and merges together matching
rows in order to generate output. The two inputs
are assumed to be sorted on the join column or
columns.
42
Summary of Operations
We have not covered all of the execution plan operations,
but we have covered some of the most common ones:
-
43
Elements of a TKPROF
Report
Report heading
TKPROF version, date run, sort option, trace file
One entry for each distinct SQL statement in trace file
Listing of SQL statement
OCI call statistics: count of parse, execute, and fetch
calls, rows processed, and time and I/O used
Parse information: parsing user, recursive depth,
library cache misses, and optimizer mode
Row source operation listing
Execution plan listing (optional)
Wait event listing (optional)
44
Elements of a TKPROF
Report
(continued)
Report Summary
OCI call statistics totals
Counts of how many statements were found in the
trace file, how many were distinct, and how many
were explained in the report.
45
46
cpu
elapsed
disk
query
current
-------- ---------- --------- --------- --------0.01
0.02
0
0
0
0.00
0.00
0
0
0
0.59
0.99
0
33633
0
-------- ---------- --------- --------- --------0.60
1.01
0
33633
0
rows
--------0
0
194
--------194
47
50
Execution Plan
--------------------------------------------------SELECT STATEMENT
GOAL: CHOOSE
SORT (ORDER BY)
NESTED LOOPS
NESTED LOOPS (OUTER)
NESTED LOOPS (OUTER)
NESTED LOOPS
TABLE ACCESS (BY INDEX ROWID) OF 'OBJ$'
INDEX (RANGE SCAN) OF 'I_OBJ2' (UNIQUE)
TABLE ACCESS (CLUSTER) OF 'TAB$'
INDEX (UNIQUE SCAN) OF 'I_OBJ#' (NON-UNIQUE)
INDEX (UNIQUE SCAN) OF 'I_OBJ1' (UNIQUE)
TABLE ACCESS (CLUSTER) OF 'SEG$'
INDEX (UNIQUE SCAN) OF 'I_FILE#_BLOCK#' (NON-UNIQUE)
TABLE ACCESS (CLUSTER) OF 'TS$'
INDEX (UNIQUE SCAN) OF 'I_TS#' (NON-UNIQUE)
51
Wrapping Up
Use EXPLAIN PLAN, queries against v$sql_plan, the
autotrace facility in SQL*Plus, or GUI tools to view
execution plans.
Use TKPROF to format SQL trace files for human
readability.
Execution plans and TKPROF reports give the DBA
and application developer a wealth of information that
can be used to make applications efficient and
perform well.
The catch: you need to know how to interpret
execution plans and TKPROF reports in order to get
any benefit from them. You also ought to know when
to use EXPLAIN PLAN versus when to query
v$sql_plan.
54
Resources
Download this slide show, the accompanying
white paper, and many other useful presentations
at:
www.dbspecialists.com/presentations
55
Contact Information
Roger Schrag
Database Specialists, Inc.
388 Market Street, Suite 400
San Francisco, CA 94111
Tel: 415/344-0500
Email: rschrag@dbspecialists.com
Web: www.dbspecialists.com
56