PerformanceThese are my personal notes that I use as a quick help in my work.
|
|
Basic steps for tuning
Define the objective of the improvement, such as measurable response time, database availability, access rate to the database and memory usage.
Some things to look for in case of bad performance:
Setup explain plan:
$ORACLE_HOME/admin/UTLXPLAN.SQL
--> creates
the table PLAN_TABLE
INSERT, SELECT, DELETE
to users that will use the utilitySetup statstics:
$ORACLE_HOME/admin/PLUSTRCE.SQL
as SYS
--> creates the role PLUSTRACE
PLUSTRACE
to users that will use autotraceUse:
EXPLAIN PLAN
[SET STATEMENT_ID = 'label']
-- Use
to flag rows
[INTO schema.table@db_link]
-- if not using the default table PLAN_TABLE
FOR statement
Some of the columns:
OPTIMIZER: Optimizer mode
POSITION: For the first row of output, indicates the CBO's cost of the statement.
For the other rows, indicates the position relative to the other children of
the same parent.
COST: cost of the operation.
CARDINALITY: number of rows accessed
BYTES: number of bytes accessed
CPU_COST: CPU cost
IO_COST: IO cost
TEMP_SPACE: temp space used
A query to see results:
ACCEPT QUERY CHAR PROMPT 'QUERY NAME: '
TTITLE 'QUERY PLAN'
COL PLAN FORMAT A80
SELECT LPAD( ' ', 2 * ( LEVEL - 1 ) ) || OPERATION || ' ' || OPTIONS || ' '
|| OBJECT_NAME || ' ' || OBJECT_TYPE PLAN,
COST
FROM PLAN_TABLE
WHERE STATEMENT_ID = '&&QUERY'
CONNECT BY PRIOR ID = PARENT_ID
AND PRIOR STATEMENT_ID
= '&QUERY'
START WITH ID =1;
UNDEFINE QUERY
SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]]
Autotrace
set autotrace trace
- Explain plan and stat (waits for execution but does not show results)
set autotrace trace exp
- Only explain plan (no statistics)
set autotrace on
- Show results, in addition to explain plan and statistics
set autotrace on explain
- Show results, in addition to explain plan
set autotrace off
- Turn off
- Script for creating table is in rdbms/admin/utlxplan.sql
Oracle 9i: select * from table(dbms_xplan.display);
Note that Oracle can combine severs bitmap indexes and can only use one non-bitmap index.
Excessive I/O on SYSTEM tablespace may indicate:
optimizer_mode initialization parameter --> rule-based (RBO) or cost-based (CBO)
The optimizer mode may be set for the instance
(at start-up), for the session (ALTER SESSION SET OPTIMIZER_MODE=...
)
or for the statement using a hint (SELECT /*+ ALL_ROWS */
)
OPTIMIZER_MODE=RULE
OPTIMIZER_MODE=ALL_ROWS
OPTIMIZER_MODE=FIRST_ROWS_n
OPTIMIZER_MODE=FIRST_ROWS
OPTIMIZER_MODE=CHOOSE
SELECT /*+ ordered */
/*+ LEADING (table_name) */
/*+ ORDERED */
/*+ STAR */
/*+ index(tab_alias, index_name) index(tab_alias2, index_name2) */
/*+ index(my_tab my_tab(col1, col2)) */
See René Nyffenegger's collection of things on the web
Hint discouraged in or on views
BITMAP_MERGE_AREA_SIZE
CURSOR_SHARING
DB_FILE_MULTIBLOCK_READ_COUNT
ALTER SESSION, ALTER SYSTEM
HASH_AREA_SIZE
2 * SORT_AREA_SIZE
HASH_JOIN_ENABLED
TRUE
. Otherwise false.OPTIMIZER_FEATURES_ENABLE Parameter
OPTIMIZER_DYNAMIC_SAMPLING
ALTER SESSION, ALTER SYSTEM
. OPTIMIZER_INDEX_CACHING
ALTER SESSION
OPTIMIZER_INDEX_COST_ADJ
ALTER SESSION
OPTIMIZER_MAX_PERMUTATIONS
OPTIMIZER_MODE
"OPTIMIZER_MODE"
PARALLEL_ADAPTIVE_MULTI_USER
PARALLEL_AUTOMATIC_TUNING
FALSE
PARTITION_VIEW_ENABLED
ALTER SESSION
FALSE
QUERY_REWRITE_ENABLED
ALTER SESSION/SYSTEM QUERY_REWRITE_ENABLED = {force | true | false}
ALTER SESSION, ALTER SYSTEM
QUERY_REWRITE_INTEGRITY
ALTER SESSION, ALTER SYSTEM
ENFORCED
SORT_AREA_RETAINED_SIZE
ALTER SESSION, ALTER SYSTEM ...DEFERRED
SORT_AREA_SIZE
SORT_AREA_SIZE
V$SYSTEM_EVENT
indicates
"Sorts to Disk" wait events, or if V$SYSSTAT (sorts-in-memory)/(sorts-in-memory
+ sorts-on-disk) ratio is < 99%. PGA_AGGREGATE_TARGET
ALTER SESSION, ALTER SYSTEM ...DEFERRED
STAR_TRANSFORMATION_ENABLED
TRUE, FALSE, TEMP_DISABLE
. ALTER SESSION
FALSE
A trace file is created in the sub-directory indicated by USER_DUMP_DEST
parameter. The maximum size of the dump file is limited by the parameter MAX_DUMP_FILE_SIZE
.
Timing results are available by changing the parameter TIMED_STATISTICS
.
Process the dump file with TKPROF
or another utility.
For the whole database:
ALTER SYSTEM set sql_trace=TRUE scope=spfile;
(9i)
Or change the parameter in init.ora.
Note that this is not dynamic --> restart the database.
For the current session (dynamic):
ALTER SESSION set sql_trace=true;
ALTER SESSION set events '10046 trace name context forever, level
8';
ALTER SESSION set events '10046 trace name context off';
For another session:
First get the sid and serial number:
select sid, serial#, username, osuser, machine
from v$session
where username is not null;
Then start the tracing (note that granting permission may be necessary):
exec sys.dbms_system.set_ev(<sid number>,<serial number>,10046,<level
(see above)>,' ');
V$SESSION
SID, SERIAL#,
USERNAME, OSUSER, MACHINE, PROGRAM, LOGON_TIME, CLIENT_INFO
V$OPEN_CURSOR
V$PROCESS
V$SEGSTAT
V$SESS_IO
PHYSICAL_READS
V$SESSION_LONGOPS
V$SQL
V$SQLAREA
V$SQLAREA
V$SQLTEXT
V$SQLTEXT_WITH_NEWLINES
V$SQLTEXT
V$SQL_PLAN - V$SQL_PLAN_STATISTICS - V$SQL_PLAN_STATISTICS_ALL
V$SYSSTAT
"select name, value
from v$sysstat order by value asc;"
V$SESSTAT
V$STATNAME
: select b.sid, a.name, b.value from v$statname a, v$sesstat b where a.statistic#=b.statistic#
V$SYSTEM_EVENT
V$SESSION_WAIT
SID
, EVENT
, and SECONDS_IN_WAIT
(see events)
Use DBMS_STATS package (recommended) or the ANALYZE statement
ANALYZE TABLE emp COMPUTE STATISTICS;
ANALYZE TABLE emp ESTIMATE STATISTICS SAMPLE { n ROWS | n PERCENT } ;
DBMS_UTILITY.ANALYZE_SCHEMA
DBMS_UTILITY.ANALYZE_DATABASE
DBMS_DDL.ANALYZE_OBJECT
PROCEDURE DBMS_UTILITY.ANALYZE_SCHEMA
(schema IN VARCHAR2
,method IN VARCHAR2
,estimate_rows IN NUMBER DEFAULT NULL
,estimate_percent IN NUMBER DEFAULT NULL
,method_opt IN VARCHAR2 DEFAULT NULL);
'FOR TABLE', 'FOR ALL COLUMNS [SIZE n]', 'FOR ALL INDEXED COLUMNS
[SIZE n]', 'FOR ALL INDEXES'
Examples
BEGIN
DBMS_UTILITY.ANALYZE_SCHEMA ( USER, 'ESTIMATE', 100, 50, 'FOR ALL INDEXES');
END;
/
BEGIN
DBMS_UTILITY.ANALYZE_SCHEMA ( user, 'COMPUTE', null, null, null);
END;
/
List of event names: select * from v$event_name
Show events since startup:
V$SYSTEM_EVENT
V$SESSION_EVENT
(join with V$SESSION
)
V$SESSION_WAIT
: current waits.
Values for wait time in V$SESSION_WAIT
:
> 0
Last wait time
= 0
Session is waiting
=-1
Value was less than 10 msec
=-2
No information available
Also see parameter TIMED_STATISTICS