Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

Oracle Performance

Tuning 101
Developer Perspective
Day 1

Karen Liu (karliu@cisco.com)


May 14, 2009

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

–Scroll down to the bottom of the page, you will find:


Database Performance
Database Lock Holders & Waiters
Database Top SQL
Database Cache Advice
Database Shared Pool Advice
Database PGA Target Advice
Database Memory Usage
Database Memory Usage By User
Database PGA/UGA Usages
Database System Wait Events
Database System Statistics
Database Latch Wait Events
Database Pending Transactions
Database Open Cursors
View Perf Files
TKPROF/Trace
Explain Plan

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: 

alter session set tracefile_identifier='a_unique_name';


alter session set sql_trace=true;
alter session set max_dump_file_size=unlimited;
alter session set events '10046 trace name context forever, level 12';
set timing on
set term off
set feedback off

-- >>>>> code to be traced >>>>>


-- EXEC pkg.proc(parm, parm) or
-- SELECT xxx FROM xxx WHERE xxx
-- <<<<< code to be traced <<<<<

set timing off


alter session set events '10046 trace name context off';
ALTER SESSION SET sql_trace = FALSE;

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 

CREATE OR REPLACE TRIGGER <username>_ses_trace_after_logon


after logon
on <username>.Schema
begin
execute immediate 'ALTER SESSION SET tracefile_identifier = "a_unique_name"';
execute immediate 'ALTER SESSION SET sql_trace = true';
execute immediate 'ALTER SESSION SET timed_statistics = true';
execute immediate 'ALTER SESSION SET max_dump_file_size = unlimited';
execute immediate 'ALTER SESSION SET events "10046 trace name context forever, level 12"';
exception
when others then
NULL;
end <username>_ disable_pq_after_logon;
/

-- ALTER TRIGGER <username>_SES_TRACE_AFTER_LOGON DISABLE;

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’);

 To enable tracing for a Module


DBMS_MONITOR.SERV_MOD_ACT_TRACE_ENABLE(
service_name => 'CRMPRD.cisco.com',
module_name => 'Summary_Pane',
action_name => DBMS_MONITOR.ALL_ACTIONS,
waits => TRUE,
binds => FALSE,
instance_name = >NULL);

 To disable tracing for a Module


DBMS_MONITOR.SERV_MOD_ACT_TRACE_DISABLE(
service_name => 'CRMPRD.cisco.com',
module_name => 'Summary_Pane');

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

 Trace file can also be found at where


USER_DUMP_DEST is pointed to on the database
server machine:

SQL> show parameters user_dump_dest


NAME TYPE VALUE
----------------- ----------- --------------------------
user_dump_dest string /oracle/admin/ESALESDV/udump

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

 To format trace file manually:

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

call count cpu elapsed disk query current rows


------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 3 0.01 0.01 0 0 0 0
Execute 3 0.03 0.02 0 0 0 0
Fetch 6 7.99 9.27 1662 150093 0 3
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 12 8.03 9.31 1662 150093 0 3

Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 13
How to Read Trace file
Execution Plan

Rows Row Source Operation


------- ---------------------------------------------------
1 SORT ORDER BY
1 TABLE ACCESS BY INDEX ROWID CMRS_ITEM_PRICING
3 NESTED LOOPS
1 NESTED LOOPS
1 NESTED LOOPS
1 NESTED LOOPS
1 TABLE ACCESS BY INDEX ROWID CMRS_PRICE_LIST_MAPPING
1 INDEX RANGE SCAN CMRS_PRICE_LIST_MAPPING_U1 (object id 3693702)
1 TABLE ACCESS BY INDEX ROWID DW_WEB_PUB_PROD_MAPS
100813 INDEX RANGE SCAN PMAP_IX4 (object id 68454)
1 TABLE ACCESS BY INDEX ROWID CMRS_ITEMS
1 INDEX RANGE SCAN CMRS_ITEMS_SPARE_IX (object id 3693802)
1 TABLE ACCESS BY INDEX ROWID DW_WEB_PUB_PROD_MAPS
17 INDEX RANGE SCAN PMAP_IX3 (object id 7029)
1 INDEX RANGE SCAN CMRS_ITEM_PRICING_IX4 (object id 3708895)

Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 14
How to Read Trace file
Wait Time

Event waited on Times Max. Wait Total Waited


----------------------------------- Waited ---------- ------------
SQL*Net message to client 6 0.00 0.00
db file sequential read 1658 0.00 0.99
global cache cr request 898 0.00 0.55
SQL*Net message from client 6 31.58 34.21

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

 If the SQL statement is UNION-ized, break it and run


each statement separately
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 17
AUTOTRACE..
Elapsed: 00:00:00.07

--------------------------------------------------------------------------------------------------------
| 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];

SQL> select table_name, num_rows, sample_size, chain_cnt, last_analyzed


2 from all_tables where table_name = 'USR_ROLE_DRES_APP_STG'
3 /

TABLE_NAME NUM_ROWS SAMPLE_SIZE CHAIN_CNT LAST_ANAL


------------------------------ ---------- ----------- ---------- ---------
USR_ROLE_DRES_APP_STG 203850 203850 0 20-OCT-08

SQL> select count(*) from USR_ROLE_DRES_APP_STG;

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];

SQL> SELECT table_name, column_name, num_distinct, num_nulls FROM


2 all_tab_columns WHERE table_name = 'USR_ROLE_DRES_APP_STG';

TABLE_NAME COLUMN_NAME NUM_DISTINCT NUM_NULLS


---------------------- ------------------------------ ------------ ----------
USR_ROLE_DRES_APP_STG PUBLISH_ID 1 0
USR_ROLE_DRES_APP_STG USER_ID 19500 0
USR_ROLE_DRES_APP_STG USER_EMAIL 17115 2130
USR_ROLE_DRES_APP_STG ROLE_ID 195 0
USR_ROLE_DRES_APP_STG ROLE_NAME 195 0
USR_ROLE_DRES_APP_STG SUBROLE_ID 0 203850
USR_ROLE_DRES_APP_STG SUBROLE_NAME 0 203850
USR_ROLE_DRES_APP_STG LVL 8 9948
USR_ROLE_DRES_APP_STG LVL_CODE 12 9948

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

index_name, -------------------------------- -------------------- ---------------


USR_ROLE_DR_APP_STG_N1 1 USER_EMAIL 1
column_name, USR_ROLE_DR_APP_STG_N1 NODE_VAL 2
column_position USR_ROLE_DR_APP_STG_N1 DATA_RES_TYPE 3
USR_ROLE_DR_APP_STG_N1 ROLE_ID 4
FROM USR_ROLE_DR_APP_STG_N2 2 APP_RES_ID 1
USR_ROLE_DR_APP_STG_N3 WORKER_ID 1
3
all_ind_columns USR_ROLE_DR_APP_STG_N4
4
USER_ID 1
USR_ROLE_DR_APP_STG_N4 TOOL_ID 2
WHERE USR_ROLE_DR_APP_STG_N4 STATUS 3
USR_ROLE_DR_APP_STG_N4 DATA_RES_TYPE 4

table_name = USR_ROLE_DR_APP_STG_N4 DATA_STRUCT_TYPE 5

[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

 Verify Cost Based Statistics up-to-date


SELECT table_name, inserts, updates, deletes
FROM user_tab_modifications;

 Verify Object Fragmentation


 Locate bottleneck in Execution Plan
 Verify Index used on most selective filter (WHERE clause)
 Verify Table join order (FROM clause)
 Eliminate/reduce wait time

Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 24
Presentation_ID © 2006 Cisco Systems, Inc. All rights reserved. Cisco Confidential 25

You might also like