Professional Documents
Culture Documents
Tools
-1-
Lesson Objectives
-2-
Overview of Data Gathering
• Database Statistics
• Operating System Statistics
• Interpreting Statistics
-3-
Oracle Optimizer: Overview
-4-
Optimizer Statistics
-5-
Using the Manage Optimizer
Statistics Page
-6-
Gathering Optimizer Statistics Manually
-7-
Statistic Levels
STATISTICS_LEVEL
Additional statistics
Self-tuning Recommended
for manual
capabilities disabled default value
SQL diagnostics
-9-
X.3: Breadcrumb
- 11 -
Measuring SQL Performance using
Diagnostic Tools
- 12 -
The SQL Trace Facility
Trace
SQL Trace file
Report
TKPROF file
Database
- 13 -
Using the SQL Trace Facility
Trace
SQL Trace file
Report
TKPROF file
Database
- 14 -
The TKPROF (Transient Kernel Profiling) Command Options
SORT = option
PRINT = n
EXPLAIN = user/password
INSERT = filename
SYS = NO
AGGREGATE = NO
RECORD = filename
TABLE = schema.tablename
- 18 -
Formatting Trace Files using TKPROF
C:\oracle\product\10.2.0\db_1\admin\orcl\udump>tkprof
orcl_ora_6492.trc a.txt
- 20 -
Output of the TKPROF Command
- 21 -
Output of the TKPROF Command
- 22 -
Row source operations provide
– the number of rows processed for each operation executed on the rows
and additional row source information, such as physical reads and
writes
In this sample TKPROF output, note the following under the Row
Source Operation column:
– cr specifies consistent reads performed by the row source
– r specifies physical reads performed by the row source
– w specifies physical writes performed by the row source
– time specifies time in microseconds
- 23 -
Output of the TKPROF Command
- 24 -
TKPROF Output Example: No Index
- 25 -
TKPROF Output Example: Unique Index
- 26 -
TKPROF Example …Contd
- 27 -
TKPROF Example …Contd
- 28 -
Analyzing tkprof Results
- 29 -
Demo
- 30 -
Creating the Plan Table
- 31 -
Using the EXPLAIN PLAN command and the
DBMS_XPLAN.DISPLAY function
OR, you can see the explain plan using DBMS_XPLAN package
select * from table(dbms_xplan.display);
- 32 -
EXPLAIN PLAN Example
Explained.
- 33 -
select job,sum(sal)from emp e, dept d where
e.deptno=d.deptno group by job;
- 34 -
Displaying the Execution Plan
- 35 -
Displaying the Execution Plan
- 36 -
Understanding the execution plan
- 37 -
Reading an Explain Plan
- 38 -
scott@ORA920> @?/rdbms/admin/utlxpls
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------
| Id | Operation |Name |Rows|Bytes|Cost |
-----------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | |
| 1 | NESTED LOOPS | | | | |
| 2 | NESTED LOOPS | | | | |
| 3 | TABLE ACCESS FULL | SALGRADE| | | |
|* 4 | TABLE ACCESS FULL | EMP | | | |
| 5 | TABLE ACCESS BY INDEX ROWID| DEPT | | | |
|* 6 | INDEX UNIQUE SCAN | DEPT_PK | | | |
-----------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - filter("EMP"."SAL"<="SALGRADE"."HISAL" AND
"EMP"."SAL">="SALGRADE"."LOSAL")
6 - access("EMP"."DEPTNO"="DEPT"."DEPTNO")
- 39 -
psuedo code for how the plan is evaluated and then we'll discuss how I
arrived at this conclusion:
For salgrade in (select * from salgrade)
Loop
For emp in ( select * from emp )
Loop
If ( emp.sal between salgrade.losal and salgrade.hisal )
Then
Select * into dept_rec
From dept
Where dept.deptno = emp.deptno;
OUTPUT RECORD with fields from salgrade,emp,dept
End if;
End loop;
End loop;
- 40 -
You should also be able to look at the execution plan and assess if the
Optimizer has made any mistake in its estimations or calculations, leading
to a suboptimal plan. The components to assess are:
Cardinality– Estimate of the number of rows coming out of each of the
operations.
Access method – The way in which the data is being accessed, via either a
table scan or index access.
Join method – The method (e.g., hash, sort-merge, etc.) used to join tables
with each other.
Join type – The type of join (e.g., outer, anti, semi, etc.).
Join order – The order in which the tables are joined to each other.
Partition pruning – Are only the necessary partitions being accessed to
answer the query?
Parallel Execution – In case of parallel execution, is each operation in the
plan being conducted in parallel? Is the right data redistribution method
being used?
- 41 -
Access Method
The access method - or access path - shows how the data will be
accessed from each table (or index).
Full table scan
– Reads all rows from a table and filters out those that do not meet the
where clause predicates.
– A FTS is selected if large portion of the rows in the table must be
accessed, no indexes exist or the ones present can’t be used or if the
cost is the lowest.
- 42 -
Access Method …Table access by ROWID
The rowid of a row specifies the data file, the data block within that
file, and the location of the row within that block.
Oracle first obtains the rowids either from a WHERE clause
predicate or through an index scan of one or more of the table's
indexes.
Oracle then locates each selected row in the table based on its
rowid and does a row-by-row access.
TABLE ACCESS (BY INDEX ROWID) and TABLE ACCESS (BY
USER ROWID
- 43 -
Access Method …Index unique scan
- 44 -
Access Method …Index range scan
Oracle accesses adjacent index entries and then uses the ROWID
values in the index to retrieve the corresponding rows from the
table.
An index will be used when a statement has an equality predicate
on a non-unique index key, or a non-equality or range predicate on
a unique index key. (=, <, >,LIKE if not on leading edge).
Data is returned in the ascending order of index columns.
– select * from emp where empno>1;
- 45 -
Access Method …Index range scan descending
- 46 -
Access Method …Index Skip Scanning
Normally, in order for an index to be used, the prefix of the index key
(leading edge of the index) would be referenced in the query.
However, if all the other columns in the index are referenced in the
statement except the first column, Oracle can do an index skip scan,
to skip the first column of the index and use the rest of it.
This can be advantageous if there are few distinct values in the
leading column of a concatenated index and many distinct values in
the non-leading key of the index.
In previous releases a composite index could only be used if the first
column, the leading edge, of the index was referenced in the
WHERE clause of a statement.
In Oracle 9i this restriction is removed because the optimizer can
perform skip scans to retrieve rowids for values that do not use the
prefix.
- 47 -
Access Method …A full index scan
A full index scan does not read every block in the index structure, contrary
to what
its name suggests.
An index full scan processes all of the leaf blocks of an index, but only
enough of
the branch blocks to find the first leaf block.
It is used when all of the columns necessary to satisfy the statement are in
the index and it is cheaper than scanning the table. It uses single block IOs.
It may be used in any of the following situations:
– An ORDER BY clause has all of the index columns in it and the order is the
same as in the index (can also contain a subset of the columns in the
index).
– The query requires a sort merge join and all of the columns referenced in
the query are in the index.
– Order of the columns referenced in the query matches the order of the
leading index columns.
– A GROUP BY clause is present in the query, and the columns in the GROUP
BY clause are present in the index.
- 48 -
A full index scan…Example
- 49 -
Join method
The join method describes how data from two data producing
operators will be joined together.
You can identify the join methods used in a SQL statement by
looking in the operations column in the explain plan.
- 50 -
Hash Joins - Hash joins are used for joining large data sets.
– The optimizer uses the smaller of the two tables or data sources to
build a hash table, based on the join key, in memory.
– It then scans the larger table, and performs the same hashing
algorithm on the join column(s).
– It then probes the previously built hash table for each value and if
they match, it returns a row.
– Smaller result set should be the driving table
– Larger result set should be the inner table
- 51 -
Nested Loops joins - Nested loops joins are useful when small
subsets of data are being joined and if there is an efficient way of
accessing the second table (for example an index look up).
– For every row in the first table (the outer table), Oracle accesses all
the rows in the second table (the inner table).
– Consider it like two embedded FOR loops.
- 52 -
Sort Merge joins
Sort merge joins are useful when the join condition between two
tables is an inequality condition such as, <, <=, >, or >=.
Sort merge joins can perform better than nested loop joins for large
data sets.
The join consists of two steps:
– Sort join operation: Both the inputs are sorted on the join key.
– Merge join operation: The sorted lists are merged together.
A sort merge join is more if there is likely to be chosen an index on
one of the tables that will eliminate one of the sorts.
- 53 -
SQL*Plus AUTOTRACE
Syntax :-
SET AUTOTRACE OFF|ON|TRACEONLY [EXPLAIN] [STATISTICS]
- 54 -
Sort Merge joins - Sort merge joins are useful when the join
condition between two tables is an inequality condition such as, <,
<=, >, or >=.
Sort merge joins can perform better than nested loop joins for large
data sets.
The join consists of two steps:
– Sort join operation: Both the inputs are sorted on the join key.
– Merge join operation: The sorted lists are merged together.
- 55 -
Cartesian join - The optimizer joins every row from one data source
with every row from the other data source, creating a Cartesian
product of the two sets.
Typically this is only chosen if the tables involved are small or if one
or more of the tables does not have a join conditions to any other
table in the statement.
Cartesian joins are not common, so it can be a sign of problem with
the cardinality estimates, if it is selected for any other reason.
Strictly speaking, a Cartesian product is not a join.
- 56 -
SQL*Plus AUTOTRACE Examples
- 57 -
SQL*Plus AUTOTRACE Examples
- 58 -
STATSPACK
- 59 -
Philosophy behind Oracle DB 10g
Automation
– Incremental steps in 9i (Advisors, Time)
– Most significant change in Performance management
– “Out of the box” setups
– GUI “hides” the complexity (and details!)
Consistency
– Common/Unified interface
– Stats storage and presentation
– Interpretation
- 60 -
Reality check before we proceed
- 61 -
EM
Demo
- 62 -
EM
Demo
- 63 -
EM
Demo
- 64 -
EM
Demo
- 65 -
Database Maintenance
Automatic Automatic
Workload Diagnostic
Repository Efficient Repository
- 66 -
AWR Infrastructure
External clients
EM SQL*Plus …
SGA
Efficient V$ DBA_*
in-memory AWR
statistics snapshots
collection MMON
Self-tuning … Self-tuning
ADDM
Internal clients component component
- 67 -
Terminology
- 68 -
AWR Contents
- 69 -
Enterprise Manager and the AWR
- 70 -
Managing the AWR
– Retention period
• Default: Eight days
• Consider storage needs
– Collection interval
• Default: 60 minutes
• Consider storage needs and performance impact
– Collection level
• Basic (disables most ADDM functionality)
• Typical (recommended)
• All (adds additional SQL tuning information to snapshots)
- 71 -
Automatic Database
Diagnostic Monitor (ADDM)
Snapshots
EM ADDM
ADDM results
AWR
- 72 -
Diagnosing Problems Automatically
- 73 -
The main sections in an AWR report include
Report Summary: This gives an overall summary of the instance during the
snapshot period, and it contains important aggregate summary information.
Cache Sizes (end): This shows the size of each SGA region after AMM has
changed them. This information can be compared to the original init.ora
parameters at the end of the AWR report.
Load Profile: This important section shows important rates expressed in
units of per second and transactions per second.
Instance Efficiency Percentages: With a target of 100%, these are high-level
ratios for activity in the SGA.
Shared Pool Statistics: This is a good summary of changes to the shared
pool during the snapshot period.
Top 5 Timed Events: This is the most important section in the AWR report.
It shows the top wait events and can quickly show the overall database
bottleneck.
- 74 -
Wait Events Statistics Section: This section shows a breakdown of the main
wait events in the database including foreground and background database
wait events as well as time model, operating system, service, and wait
classes statistics.
Wait Events: This AWR report section provides more detailed wait event
information for foreground user processes which includes Top 5 wait events
and many other wait events that occurred during the snapshot interval.
Background Wait Events: This section is relevant to the background process
wait events.
Time Model Statistics: Time mode statistics report how database-processing
time is spent. This section contains detailed timing information on particular
components participating in database processing.
Operating System Statistics: The stress on the Oracle server is important,
and this section shows the main external resources including I/O, CPU,
memory, and network usage.
Service Statistics: The service statistics section gives information about how
particular services configured in the database are operating.
- 75 -
SQL Section: This section displays top SQL,
ordered by important SQL execution metrics.
SQL Ordered by Elapsed Time: Includes SQL statements that took
significant execution time during processing.
SQL Ordered by CPU Time: Includes SQL statements that consumed
significant CPU time during its processing.
SQL Ordered by Gets: These SQLs performed a high number of
logical reads while retrieving data.
SQL Ordered by Reads: These SQLs performed a high number of
physical disk reads while retrieving data.
SQL Ordered by Parse Calls: These SQLs experienced a high
number of reparsing operations.
SQL Ordered by Sharable Memory: Includes SQL statements cursors
which consumed a large amount of SGA shared pool memory.
SQL Ordered by Version Count: These SQLs have a large number of
versions in shared pool for some reason.
- 76 -
Instance Activity Stats: This section contains statistical
information describing how the database operated during the
snapshot period.
Instance Activity Stats (Absolute Values): This section
contains statistics that have absolute values not derived from
end and start snapshots.
Instance Activity Stats (Thread Activity): This report
section reports a log switch activity statistic.
- 77 -
I/O Section: This section shows the all important I/O activity for
the instance and shows I/O activity by tablespace, data file, and
includes buffer pool statistics.
Tablespace IO Stats
File IO Stats
Buffer Pool Statistics
- 78 -
Advisory Section: This section show details of the advisories
for the buffer, shared pool, PGA and Java pool.
Buffer Pool Advisory
PGA Aggr Summary: PGA Aggr Target Stats; PGA Aggr
Target Histogram; and PGA Memory Advisory.
Shared Pool Advisory
Java Pool Advisory
- 79 -
Buffer Wait Statistics: This important section shows buffer
cache waits statistics.
Enqueue Activity: This important section shows how enqueue
operates in the database. Enqueues are special internal
structures which provide concurrent access to various
database resources.
Undo Segment Summary: This section gives a summary about
how undo segments are used by the database.
Undo Segment Stats: This section shows detailed history
information about undo segment activity.
- 80 -
Latch Activity: This section shows details about latch
statistics. Latches are a lightweight serialization mechanism
that is used to single-thread access to internal Oracle
structures.
Latch Sleep Breakdown
Latch Miss Sources
Parent Latch Statistics
Child Latch Statistics
- 81 -
Segment Section: This report section
provides details about hot segments using
the following criteria:
- 82 -
Dictionary Cache Stats: This section exposes details about
how the data dictionary cache is operating.
Library Cache Activity: Includes library cache statistics
describing how shared library objects are managed by Oracle.
SGA Memory Summary: This section provides summary
information about various SGA regions.
init.ora Parameters: This section shows the original init.ora
parameters for the instance during the snapshot period.
- 83 -
Some Examples on reading AWR
- 84 -
ADDM Page
- 85 -
SQL Details
- 86 -
Topology Viewer for SQL Details Plan
- 87 -
Session Details
- 88 -
Database Performance Page
- 89 -
Database Performance Page
- 90 -
Real-Time Monitoring Links
- 91 -
Top Activity
- 92 -
Top Consumers
- 94 -
Instance Activity
- 95 -
Historical SQL (AWR)
- 96 -
Active Session History (ASH) Report
- 97 -
Use Active Session History (ASH) reports to perform analysis
of:
– Transient performance problems that typically last for a few minutes
– Scoped or targeted performance analysis by various dimensions or their
combinations, such as time, session, module, action, or SQL_ID
- 98 -
ADDM Findings
- 99 -
ADDM Recommendations
- 100 -
ADDM
Demo
- 101 -
Automated Maintenance Tasks
- 102 -
Setting Thresholds
- 103 -
Creating and Testing an Alert
1. Specify a threshold.
2. Create a test case.
3. Check for an alert.
1
2
- 104 -
Alerts Notification
- 105 -
AWR – Performance Warehouse
- 107 -
ASH – What’s up with sessions
- 108 -
ASH – Session states exposed!
“On-the-spot” analysis
Retroactive analysis
– From memory buffer (V$ACTIVE_SESSION_HISTORY)
– From persisted AWR data (WRH$_ACTIVE_SESSION_HISTORY connected via
SNAP_ID)
– Supports manual drill down from AWR/ADDM
Tracks High load SQL execution behavior
Determine Blocking sessions and “hot” segments
SESSION_STATE : “ON CPU” or “WAITING”
- 109 -
Useful sections: AWR Report
- 110 -
Extracting data from the AWR
- 111 -
Extracting data from the AWR
- 112 -
Extracting data from the AWR
- 113 -
Extracting data from the AWR
- 114 -
New features in DBMS_XPLAN
- 115 -
New features in DBMS_XPLAN
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('10.2.0.3')
OPT_PARAM('_b_tree_bitmap_plans' 'false')
OPT_PARAM('_fast_full_scan_enabled' 'false')
*/
- 116 -
New features in DBMS_XPLAN
1 - :1 (NUMBER): 50357
2 - :1 (NUMBER, Primary=1)
1 - "T"."RESPONSIBILITY_NAME"[VARCHAR2,100]
3 - "T"."RESPONSIBILITY_NAME"[VARCHAR2,100]
4 - "T".ROWID[ROWID,10]
- 117 -
V$ Performance Views
- 118 -
Wait Events
- 119 -
Oracle Wait Events
- 120 -
Time Model Statistics
- 121 -
V$EVENT_NAME View
- 122 -
Statistics Event Views
- 123 -
V$SYSTEM_EVENT View
- 124 -
V$SESSION_EVENT View
- 125 -
V$SESSION_WAIT View
- 126 -
The following query shows the distribution of shared pool
memory across different type of objects. It also shows if any of
the objects have been pinned in the shared pool using the
procedure DBMS_SHARED_POOL.KEEP().
- 127 -
Finding Objects with Large Number of Loads
- 128 -