收集常用SQL调优语句
1,PGA性能指标
SELECT NAME,
VALUE,
100 *
(VALUE / DECODE((SELECT SUM(VALUE)
FROM V$SYSSTAT
WHERE NAME LIKE 'workarea executions%'),
0,
NULL,
(SELECT SUM(VALUE)
FROM V$SYSSTAT
WHERE NAME LIKE 'workarea executions%'))) PCT
FROM V$SYSSTAT
WHERE NAME LIKE 'workarea executions%';
2,定位有问题的SQL语句
SELECT /*+ ORDERED */
SQL_TEXT
FROM V$SQLTEXT A
WHERE (A.HASH_VALUE, A.ADDRESS) IN
(SELECT DECODE(SQL_HASH_VALUE, 0, PREV_HASH_VALUE, SQL_HASH_VALUE),
DECODE(SQL_HASH_VALUE, 0, PREV_SQL_ADDR, SQL_ADDRESS)
FROM V$SESSION B
WHERE B.PADDR =
(SELECT ADDR FROM V$PROCESS C WHERE C.SPID = '&pid'))
ORDER BY PIECE ASC;
&pid写系统消耗CPU高的进程。
3,查询用户及相应配置文件,默认的表空间和临时表空间
SELECT USERNAME, PROFILE, DEFAULT_TABLESPACE, TEMPORARY_TABLESPACE, CREATED
FROM DBA_USERS
ORDER BY USERNAME;
4,指定等待事件
SELECT /*+ ordered */
SID, EVENT, OWNER, SEGMENT_NAME, SEGMENT_TYPE, P1, P2, P3
FROM V$SESSION_WAIT SW, DBA_EXTENTS DE
WHERE DE.FILE_ID = SW.P1
AND SW.P2 BETWEEN DE.BLOCK_ID AND DE.BLOCK_ID + DE.BLOCKS - 1
AND (EVENT = 'buffer busy waits' OR EVENT = 'write complete waits')
AND P1 IS NOT NULL
ORDER BY EVENT, SID;
最后10个等待事件
SELECT /*+ ordered */
SID, EVENT, OWNER, SEGMENT_NAME, SEGMENT_TYPE, P1, P2, P3
FROM V$SESSION_WAIT_history SW, DBA_EXTENTS DE
WHERE DE.FILE_ID = SW.P1
AND SW.P2 BETWEEN DE.BLOCK_ID AND DE.BLOCK_ID + DE.BLOCKS - 1
AND (EVENT = 'buffer busy waits' OR EVENT = 'write complete waits')
AND P1 IS NOT NULL
ORDER BY EVENT, SID;
查找p1,p2,p3代表什么
SELECT EVENT#, NAME, PARAMETER1 P1, PARAMETER2 P2, PARAMETER3 P3
FROM V$EVENT_NAME
WHERE NAME IN ('buffer busy waits', 'write complete waits');
会话开始后所有的等待
SELECT SID, EVENT, TOTAL_WAITS, TIME_WAITED, EVENT_ID
FROM V$SESSION_EVENT
WHERE TIME_WAITED > 0
ORDER BY TIME_WAITED DESC;
5.检查每个会话正在执行的SQL语句
SELECT A.SID, A.USERNAME, B.SQL_TEXT
FROM V$SESSION A
JOIN V$SQLTEXT B
ON A.SQL_ADDRESS = B.ADDRESS
AND A.SQL_HASH_VALUE = B.HASH_VALUE
ORDER BY A.USERNAME, A.SID, B.PIECE;
6.查看那些用户消耗大量的物理磁盘和内存读操作
SELECT A.USERNAME,
B.BLOCK_GETS,
B.CONSISTENT_GETS,
B.PHYSICAL_READS,
B.BLOCK_CHANGES,
B.CONSISTENT_CHANGES
FROM V$SESSION A
JOIN V$SESS_IO B
ON A.SID = B.SID
ORDER BY A.USERNAME;
7,关闭有问题的会话
SID,serial#来自v$session
ALTER SYSTEM KILL SESSION ''sid','serial#'';
8,ORA-04031
shared_pool_size或则java_pool_size设置太小,共享池出现碎片化,或者应用程序代码可能无法实现优化共享。
大量的硬解析,可以考虑设置cursor_sharing参数为force
SELECT NAME, VALUE
FROM V$SYSSTAT
WHERE NAME = 'parse count (total)'
OR NAME = 'parse count (hard)';
--硬解析比率
SELECT ROUND((B.VALUE / A.VALUE), 2) * 100 || '%' HARDPARSERATIO
FROM V$SYSSTAT A
JOIN V$SYSSTAT B
ON A.NAME = 'parse count (total)'
AND B.NAME = 'parse count (hard)';
SELECT SQL_TEXT, PARSE_CALLS, EXECUTIONS
FROM V$SQLAREA
WHERE PARSE_CALLS > 100
AND KEPT_VERSIONS = 0
AND EXECUTIONS < 2 * PARSE_CALLS;
库缓存命中率
SELECT ROUND(SUM(PINHITS) / SUM(PINS), 2) * 100 || '%' "Library Cache Hit Ratio"
FROM V$LIBRARYCACHE
ORDER BY NAMESPACE;
库缓存重载率
SELECT NAMESPACE,
ROUND(DECODE(PINS, 0, 0, RELOADS / PINS), 2) * 100 || '%' "Reload Ratio"
FROM V$LIBRARYCACHE;
9,oracle实例或者数据库中的多个结构可以被转存到一个跟踪文件,以备底层的分析使用:
控制文件
数据文件的文件头
重做日志文件的头信息
实例状态
进程状态
库缓存
数据块
重做块
ALTER SESSION SET EVENTS 'immediate trace name CONTROLF level 10 ';
ALTER SESSION SET EVENTS 'immediate trace name FILE_HDRS level 10';
ALTER SESSION SET EVENTS 'immediate trace name REDOHDR level 10';
ALTER SESSION SET EVENTS 'immediate trace name SYSTEMSTATE level 10';
ALTER SESSION SET EVENTS 'immediate trace name PROCESSSTATE level 10';
ALTER SESSION SET EVENTS 'immediate trace name library_cache level 10';
ALTER SYSTEM DUMP DATAFILE 10 BLOCK 2057;
ALTER SYSTEM DUMP LOGFILE '<logfilename>';