Professional Documents
Culture Documents
Oracle Performance Tuning 101 - Developer Perspective 090528 - 1
Oracle Performance Tuning 101 - Developer Perspective 090528 - 1
Tuning 101
Developer Perspective
Day 1
071024 © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 1
Agenda
Performance Tuning
Common Performance Problems (day 1)
Diagnostic Tools (day 1)
How to Identify SQL Performance Issues (day 1)
Best Practice
SQL Statements (day 2)
Schema Objects (day 3)
PL/SQL Logic (day 3)
Data Warehouse/OLTP (day 3)
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 2
Performance Tuning
Common Performance Problems
Diagnostic Tools
How to identify SQL Performance Issues
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 3
Logical steps in Oracle Tuning
Application
SQL, PL/SQL, Physical/Logical Data Model
Database
Locks, Latches, Buffers, Sequences, Redo/Archive logs, Shared
Servers/PQO servers, Global Cache (RAC) contention
Memory
SGA, PGA, Shared pool, DB Cache
IO Subsystem
Disks, SAME (Stripe and Mirror Everything), ASM (Automatic
Storage Management)
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 4
Common Performance Problems
Cost-based Statistics not up-to-date
Necessary index missing
Wrong table join order
Too many table joins than necessary
Too many executions than necessary
Too many table rows returned than necessary
Sort unnecessarily
Single row process in loops
Fragmentation
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 5
Diagnostic Tools
- to Find Out Performance Bottleneck
TOAD SQL TRACE
Elapsed Time Long running SQLs from an APP
Explain Plan Elapsed Times
Database Objects Logical Read
Physical Read
Database Tools Website Execution Plans
Find out primary DBA for a database
Wait Events and Time waited
Database Active Sessions
Active SQL Statements Total SQLs processing time
(I/O,Memory consumption, Explain)
Database Top SQLs AUTOTRACE
View Perf Files (AWR Reports) SQL Execution Plan
TKPROF/Trace Logical, Physical I/O and Cost
Explain Plan
EXPLAIN
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 6
Database Tools Website
The Cisco Self Service Website for Application Team provides
many useful tools to monitor database activities. Please make a
bookmark:
http://wwwin.cisco.com/it/dba/std-db_tool.shtml
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 7
How to Turn On SQL Trace
– SQL tracing can be initiated and terminated from a
SQL session by:
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 8
How to Turn On SQL Trace…
– Create a logon trigger to trace all sessions for a
schema user
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 9
How to Turn On SQL Trace for a Module
Insert Module information in PL/SQL procedure
DBMS_APPLICATION_INFO.SET_MODULE(
module_name=> ’Summary_Pane’,
action_name=> ’insert into emp’);
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 10
Where to Find Trace file
Trace file can be found at the Cisco DBA website:
Database Performance ->TKPROC/Trace
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 11
How to Format Trace file
Trace file can be formatted at the Cisco DBA website:
Database Performance ->TKPROC/Trace
Click on the trace file
Click on the TKPROF link on top
tkprof sid_ora_processid_<trace_identifier>.trc \
~/<outfile>.txt \
explain=uid/pw@sid \
sys=no \
waits=y \
aggregrate=yes \
sort=exeela fchela prsela
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 12
How to Read Trace file
Sample Trace file fragments…
Stats
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 13
How to Read Trace file
Execution Plan
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 14
How to Read Trace file
Wait Time
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 15
How to Read Trace file
Things to watch out for
Stats
Long elapsed time (in sec)
Excessive number of Executions
High disk I/O
High Logical Reads
Execution Plans
TABLE ACCESS FULL
INDEX FULL SCAN
MERGE JOIN CARTESIAN
INDEX SKIP SCAN
Excessive number of rows accessed
Wait Events
Largest wait categories represent greatest
opportunities for optimization
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 16
AUTOTRACE
AUTOTRACE is useful in tuning single SQL statement
To turn on AUTOTRACE in SQL*Plus:
SET HEADING OFF
SET TIMING ON
SET AUTOTRACE [ON|TRACEONLY]
ON: displays the result set and the explain plan
and statistics
TRACEONLY: displays the explain plan and the
statistics
Run SQL statement
Note: Substitute bind variables with literal values
--------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 36 | 9 (23)| 00:00:01 |
| 1 | HASH GROUP BY | | 1 | 36 | 9 (23)| 00:00:01 |
|* 2 | HASH JOIN | | 12 | 432 | 8 (13)| 00:00:01 |
| 3 | TABLE ACCESS BY INDEX ROWID| CN_PERIOD_STATUSES_ALL | 1 | 18 | 2 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | CN_PERIOD_STATUSES_U1 | 1 | | 1 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | CN_PERIOD_STATUSES_ALL | 180 | 3240 | 5 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
17 consistent gets
10 physical reads
0 redo size
689 bytes sent via SQL*Net to client
469 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 18
EXPLAIN PLAN
To display SQL explain plan without executing the
SQL:
EXPLAIN PLAN [INTO plan_table_name] FOR
SQL_statement_text;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
-------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
-------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 184 | 41 |
| 1 | SORT AGGREGATE | | 1 | 184 | |
| 2 | FILTER | | | | |
| 3 | TABLE ACCESS BY INDEX ROWID | USR_ROLE_DRES_APP_STG | 581 | 104K| 40 |
| 4 | BITMAP CONVERSION TO ROWIDS| | | | |
| 5 | BITMAP INDEX SINGLE VALUE | USR_ROLE_DRES_APP_S_IDX2 | | | |
| 6 | FILTER | | | | |
| 7 | TABLE ACCESS BY INDEX ROWID| USR_ROLE_DRES_APP | 1 | 280 | 1 |
| 8 | INDEX RANGE SCAN | USR_ROLE_DRES_APP_N1 | 1 | | 3 |
-------------------------------------------------------------------------------------------------
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 19
Data Dictionary Views
– helpful to investigate SQL performance
While evaluating a problem SQL statement the
following Static Data Dictionary Views provide useful
information:
ALL_TABLES - Table Statistics
ALL_TAB_COLUMNS - Column Statistics
ALL_IND_COLUMNS - Index Statistics
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 20
Table Statistics
SELECT table_name, num_rows, sample_size, chain_cnt,
last_analyzed
FROM all_tables WHERE table_name = [table_name];
COUNT(*)
----------
1
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 21
Column Statistics
SELECT table_name, column_name, num_distinct, num_nulls
FROM all_tab_columns WHERE table_name = [table_name];
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 22
Index Statistics
SQL> SELECT index_name, column_name, column_position FROM
SELECT all_ind_columns WHERE table_name =
'USR_ROLE_DRES_APP_STG' order by 1, 3;
table_name, INDEX_NAME COLUMN_NAME COLUMN_POSITION
[table_name] USR_ROLE_DR_APP_STG_N4
USR_ROLE_DR_APP_STG_N5
LVL
USER_ID
6
1
5
ORDER BY 1,2,4; USR_ROLE_DR_APP_STG_N6
USR_ROLE_DR_APP_STG_N7
6
DATA_STRUCT_TYPE
TOOL_ID
1
1
USR_ROLE_DR_APP_STG_N7
7 STATUS 2
USR_ROLE_DR_APP_STG_N7 DATA_RES_TYPE 3
USR_ROLE_DR_APP_STG_N7 DATA_STRUCT_TYPE 4
USR_ROLE_DR_APP_STG_N8 STATUS 1
8
USR_ROLE_DR_APP_STG_N8 DATA_RES_TYPE 2
USR_ROLE_DR_APP_STG_N8 DATA_STRUCT_TYPE 3
ROLE_DRES_APP_SG_BM_ST 9 STATUS 1
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 23
Checklist for SQL Tuning
Collect Information
SQL statement, Execution Plan, Table/index/column stats
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 24
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 25