| |
 |
|
Gathering SQL execution details
Oracle Tips by Burleson Consulting
Match 5, 2008
|
In addition to TKPROF (SQL*Trace) and
the SQL*Plus "set autotrace on" command, we see new tools for getting
additional SQL execution information.
Staring with Oracle9i, we see several exciting new execution plan
columns and you can ruin specialized queries to see estimates of
resource usage associated with specific steps of Oracle SQL
execution:
-
CPU_COST - The CPU cost of the operation
as estimated by the optimizer's cost-based approach. For
statements that use the rule-based approach, this column is
null. The value of this column is proportional to the number of
machine cycles required for the operation.
-
IO_COST - The I/O cost of the operation
as estimated by the optimizer's cost-based approach. For
statements that use the rule-based approach, this column is
null. The value of this column is proportional to the number of
data blocks read by the operation
-
TEMP_SPACE - The temporary space, in
bytes, used by the operation as estimated by the optimizer's
cost-based approach. For statements that use the rule-based
approach, or for operations that don't use any temporary space,
this column is null.
In Oracle 10g, also see
gather_plan_statistics hint
tips where you can gather estimated rows vs. actual rows. Oracle claims to use this
information to make more intelligent choices of execution plans, but
there is a serious problem with this approach.
1 – The CBO has no a-priori
knowledge of the data buffer contents – Because of this
shortcoming, the CBO cannot know if the data blocks are already in
the RAM data buffers.
2 – CPU costs depend upon
system load – The CPU costs associated with servicing an Oracle
query depend upon the server load, and CPU costs are generally not
important unless the entire Oracle instance is using excessive CPU
resources.
3 – Temporary segment space is
transient – Even though the CBO estimates the TEMP_SPACE costs
at optimization time, this execution plan will become permanent for
the SQL query until the executable is flushed from the library
cache. Hence, obsolete execution plans may be used to execute
a query.
Oracle also has two new views
to show active work area space, the v$sql_workarea and the
v$sql_workarea_active views. The v$sql_workarea_active
view will display all of the work areas that are currently executing
in the instance. Note that small sorts (under 65,535 bytes)
are excluded from the view. The v$sql_workarea_active
view can be used to quickly monitor the size of all large active
work areas.
workarea.sql
select to_number(decode(SID, 65535, NULL, SID)) sid,
operation_type OPERATION, trunc(WORK_AREA_SIZE/1024)
WSIZE, trunc(EXPECTED_SIZE/1024) ESIZE,
trunc(ACTUAL_MEM_USED/1024) MEM, trunc(MAX_MEM_USED/1024) "MAX
MEM", number_passes PASS from
v$sql_workarea_active order by 1,2;
Here is a sample listing from this script, showing
the work area estimated vs. actual size, plus the memory used for
the specific operation. This is a great way to see the actual
RAM used in a sort or hash join operation.
SID OPERATION
WSIZE ESIZE MEM MAX MEM PASS --- --------------------- -----
--------- --------- --------- ---- 27 GROUP BY (SORT) 73
73 64 64 0 44 HASH-JOIN 3148
3147 2437 6342 1 71 HASH-JOIN 13241 19200
12884 34684 1
For more details on gathering run-time SQL execution
statistics see my book "Oracle
Tuning: The Definitive Reference".
 |
If you like Oracle tuning, you
might enjoy my book "Oracle
Tuning: The Definitive Reference", with 950 pages of tuning tips and
scripts.
You can buy it direct from the publisher for 30%-off and get instant
access to the code depot of Oracle tuning scripts. |
|