oracle数据库性能监控的SQL
1. 监控事例的等待
SQL> SELECT EVENT,SUM(DECODE(WAIT_TIME,0,0,1)) "PREV",SUM(DECODE(WAIT_TIME,0,1,0)) "CURR",COUNT(*) "TOT" FROM V$SESSION_WAIT GROUP BY EVENT ORDER BY 4;
2. 回滚段的争用情况
SQL> SELECT NAME, WAITS, GETS, WAITS/GETS "RATIO" FROM V$ROLLSTAT A, V$ROLLNAME B WHERE A.USN = B.USN;
3. 监控表空间的 I/O 比例
SQL> SELECT DF.TABLESPACE_NAME NAME,DF.FILE_NAME "FILE",F.PHYRDS PYR,
F.PHYBLKRD PBR,F.PHYWRTS PYW, F.PHYBLKWRT PBW FROM V$FILESTAT F, DBA_DATA_FILES DF WHERE F.FILE# = DF.FILE_ID
ORDER BY DF.TABLESPACE_NAME;
4. 监控文件系统的 I/O 比例
SQL> SELECT SUBSTR(A.FILE#,1,2) "#", SUBSTR(A.NAME,1,30) "NAME",
A.STATUS,A.BYTES,B.PHYRDS,B.PHYWRTS FROM V$DATAFILE A, V$FILESTAT B
WHERE A.FILE# = B.FILE#;
5.在某个用户下找所有的索引
SQL> SELECT USER_INDEXES.TABLE_NAME, USER_INDEXES.INDEX_NAME,UNIQUENESS, COLUMN_NAME FROM USER_IND_COLUMNS, USER_INDEXES WHERE USER_IND_COLUMNS.INDEX_NAME = USER_INDEXES.INDEX_NAME
AND USER_IND_COLUMNS.TABLE_NAME = USER_INDEXES.TABLE_NAME
ORDER BY USER_INDEXES.TABLE_TYPE, USER_INDEXES.TABLE_NAME,
USER_INDEXES.INDEX_NAME, COLUMN_POSITION;
6. 监控 SGA 的命中率
SQL> SELECT A.VALUE + B.VALUE "LOGICAL_READS", C.VALUE "PHYS_READS",
ROUND(100 * ((A.VALUE+B.VALUE)-C.VALUE) / (A.VALUE+B.VALUE)) "BUFFER HIT RATIO" FROM V$SYSSTAT A, V$SYSSTAT B, V$SYSSTAT C WHERE A.STATISTIC# = 38 AND B.STATISTIC# = 39 AND C.STATISTIC# = 40;
7. 监控 SGA 中字典缓冲区的命中率
SQL> SELECT PARAMETER, GETS,GETMISSES , GETMISSES/(GETS+GETMISSES)*100 "MISS RATIO",(1-(SUM(GETMISSES)/ (SUM(GETS)+SUM(GETMISSES))))*100 "HIT RATIO" FROM V$ROWCACHE WHERE GETS+GETMISSES <>0 GROUP BY PARAMETER, GETS, GETMISSES;
8. 监控 SGA 中共享缓存区的命中率,应该小于1%
SQL> SELECT SUM(PINS) "TOTAL PINS", SUM(RELOADS) "TOTAL RELOADS",
SUM(RELOADS)/SUM(PINS) *100 LIBCACHE FROM V$LIBRARYCACHE;
SQL> SELECT SUM(PINHITS-RELOADS)/SUM(PINS) "HIT RADIO",SUM(RELOADS)/SUM(PINS) "RELOAD PERCENT" FROM V$LIBRARYCACHE;
9. 显示所有数据库对象的类别和大小
SQL> SELECT COUNT(NAME) NUM_INSTANCES ,TYPE ,SUM(SOURCE_SIZE) SOURCE_SIZE,SUM(PARSED_SIZE) PARSED_SIZE ,SUM(CODE_SIZE) CODE_SIZE ,SUM(ERROR_SIZE) ERROR_SIZE,SUM(SOURCE_SIZE) +SUM(PARSED_SIZE) +SUM(CODE_SIZE) +SUM(ERROR_SIZE) SIZE_REQUIRED FROM DBA_OBJECT_SIZE GROUP BY TYPE ORDER BY 2;
10. 监控 SGA 中重做日志缓存区的命中率,应该小于1%
SQL> SELECT NAME,
GETS,
MISSES,
IMMEDIATE_GETS,
IMMEDIATE_MISSES,
DECODE(GETS, 0, 0, MISSES / GETS * 100) RATIO1,
DECODE(IMMEDIATE_GETS + IMMEDIATE_MISSES,
0,
0,
IMMEDIATE_MISSES / (IMMEDIATE_GETS + IMMEDIATE_MISSES) * 100) RATIO2
FROM V$LATCH
WHERE NAME IN ('REDO ALLOCATION', 'REDO COPY');
11. 监控内存和硬盘的排序比率,最好使它小于 .10,增加 sort_area_size
SQL> SELECT NAME, VALUE FROM V$SYSSTAT WHERE NAME IN ('SORTS (MEMORY)', 'SORTS (DISK)');
12. 监控当前数据库谁在运行什么SQL语句
SQL> SELECT OSUSER, USERNAME, SQL_TEXT FROM V$SESSION A, V$SQLTEXT B
WHERE A.SQL_ADDRESS =B.ADDRESS ORDER BY ADDRESS, PIECE;
13. 监控字典缓冲区
SQL>SELECT (SUM(PINS - RELOADS)) / SUM(PINS) "LIB CACHE" FROM V$LIBRARYCACHE;
SQL>SELECT (SUM(GETS - GETMISSES - USAGE - FIXED)) / SUM(GETS) "ROW CACHE" FROM V$ROWCACHE;
SQL>SELECT SUM(PINS) "EXECUTIONS", SUM(RELOADS) "CACHE MISSES WHILE EXECUTING" FROM V$LIBRARYCACHE;(后者除以前者,此比率小于1%,接近0%为好)
SQL>SELECT SUM(GETS) "DICTIONARY GETS",SUM(GETMISSES) "DICTIONARY CACHE GET MISSES" FROM V$ROWCACHE;
14. 查找ORACLE字符集
SQL>SELECT * FROM SYS.PROPS$ WHERE NAME='NLS_CHARACTERSET';
15. 监控 MTS
SQL>SELECT BUSY/(BUSY+IDLE) "SHARED SERVERS BUSY" FROM V$DISPATCHER;
(此值大于0.5时,参数需加大)
SQL>SELECT SUM(WAIT)/SUM(TOTALQ) "DISPATCHER WAITS" FROM V$QUEUE WHERE TYPE='DISPATCHER';
SQL>SELECT COUNT(*) FROM V$DISPATCHER;
SQL>SELECT SERVERS_HIGHWATER FROM V$MTS;
(servers_highwater接近mts_max_servers时,参数需加大)
16. 碎片程度
SQL>SELECT TABLESPACE_NAME,COUNT(TABLESPACE_NAME) FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME HAVING COUNT(TABLESPACE_NAME)>10;
SQL>ALTER TABLESPACE NAME COALESCE;
SQL>ALTER TABLE NAME DEALLOCATE UNUSED;
SQL>CREATE OR REPLACE VIEW TS_BLOCKS_V AS
SELECT TABLESPACE_NAME,BLOCK_ID,BYTES,BLOCKS,'FREE SPACE' SEGMENT_NAME FROM DBA_FREE_SPACE
UNION ALL
SELECT TABLESPACE_NAME,BLOCK_ID,BYTES,BLOCKS,SEGMENT_NAME FROM DBA_EXTENTS;
SELECT * FROM TS_BLOCKS_V;
SQL>SELECT TABLESPACE_NAME,SUM(BYTES),MAX(BYTES),COUNT(BLOCK_ID) FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME;
查看碎片程度高的表
SQL>SELECT SEGMENT_NAME TABLE_NAME,COUNT(*) EXTENTS
FROM DBA_SEGMENTS WHERE OWNER NOT IN ('SYS', 'SYSTEM') GROUP BY SEGMENT_NAME HAVING COUNT(*)=(SELECT MAX(COUNT(*)) FROM DBA_SEGMENTS GROUP BY SEGMENT_NAME);
17. 表、索引的存储情况检查
SQL>SELECT SEGMENT_NAME,SUM(BYTES),COUNT(*) EXT_QUAN FROM DBA_EXTENTS WHERE TABLESPACE_NAME='&TABLESPACE_NAME' AND SEGMENT_TYPE='TABLE' GROUP BY TABLESPACE_NAME,SEGMENT_NAME;
SQL>SELECT SEGMENT_NAME,COUNT(*) FROM DBA_EXTENTS WHERE SEGMENT_TYPE='INDEX' AND OWNER='&OWNER' GROUP BY SEGMENT_NAME;
18、找使用CPU多的用户session
SQL>SELECT A.SID,SPID,STATUS,SUBSTR(A.PROGRAM,1,40) PROG,A.TERMINAL,OSUSER,VALUE/60/100 VALUE FROM V$SESSION A,V$PROCESS B,V$SESSTAT C
WHERE C.STATISTIC#=12 AND C.SID=A.SID AND A.PADDR=B.ADDR ORDER BY VALUE DESC;
表空间统计
A、 脚本说明:
这是我最常用的一个脚本,用它可以显示出数据库中所有表空间的状态,如表空间的大小、已使用空间、使用的百分比、空闲空间数及现在表空间的最大块是多大。
B、脚本原文:
SELECT UPPER(F.TABLESPACE_NAME) "表空间名"
D.TOT_GROOTTE_MB "表空间大小(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空间(M)",
TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),'990.99') "使用比",
F.TOTAL_BYTES "空闲空间(M)",
F.MAX_BYTES "最大块(M)"
FROM
(SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES)/(1024*1024),2) TOTAL_BYTES,
ROUND(MAX(BYTES)/(1024*1024),2) MAX_BYTES
FROM SYS.DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME, ROUND(SUM(DD.BYTES)/(1024*1024),2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 4 DESC;
查看无法扩展的段
A、 脚本说明:
ORACLE对一个段比如表段或索引无法扩展时,取决的并不是表空间中剩余的空间是多少,而是取于这些剩余空间中最大的块是否够表比索引的“NEXT”值大,所以有时一个表空间剩余几个G的空闲空间,在你使用时ORACLE还是提示某个表或索引无法扩展,就是由于这一点,这时说明空间的碎片太多了。这个脚本是找出无法扩展的段的一些信息。
B、脚本原文:
SELECT SEGMENT_NAME,
SEGMENT_TYPE,
OWNER,
A.TABLESPACE_NAME "TABLESPACENAME",
INITIAL_EXTENT/1024 "INITAL_EXTENT(K)",
NEXT_EXTENT/1024 "NEXT_EXTENT(K)",
PCT_INCREASE,
B.BYTES/1024 "TABLESPACE MAX FREE SPACE(K)",
B.SUM_BYTES/1024 "TABLESPACE TOTAL FREE SPACE(K)"
FROM DBA_SEGMENTS A,
(SELECT TABLESPACE_NAME,MAX(BYTES) BYTES,SUM(BYTES) SUM_BYTES FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME) B
WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME AND NEXT_EXTENT>B.BYTES
ORDER BY 4,3,1;
查看段(表段、索引段)所使用空间的大小
A、 脚本说明:
有时你可能想知道一个表或一个索引占用多少M的空间,这个脚本就是满足你的要求的,把<>中的内容替换一下就可以了。
B、脚本原文:
SELECT OWNER,
SEGMENT_NAME,
SUM(BYTES)/1024/1024
FROM DBA_SEGMENTS
WHERE OWNER=
AND SEGMENT_NAME=
GROUP BY OWNER,SEGMENT_NAME
ORDER BY 3 DESC;
查看数据库中的表锁
A、 脚本说明:
这方面的语句的样式是很多的,各式一样,不过我认为这个是最实用的,不信你就用一下,无需多说,锁是每个DBA一定都涉及过的内容,当你相知道某个表被哪个session锁定了,你就用到了这个脚本。
B、脚本原文
SELECT A.OWNER,
A.OBJECT_NAME,
B.XIDUSN,
B.XIDSLOT,
B.XIDSQN,
B.SESSION_ID,
B.ORACLE_USERNAME,
B.OS_USER_NAME,
B.PROCESS,
B.LOCKED_MODE,
C.MACHINE,
C.STATUS,
C.SERVER,
C.SID,
C.SERIAL#,
C.PROGRAM
FROM ALL_OBJECTS A,
V$LOCKED_OBJECT B,
SYS.GV_$SESSION C
WHERE ( A.OBJECT_ID = B.OBJECT_ID )
AND (B.PROCESS = C.PROCESS )
-- AND
ORDER BY 1,2 ;
处理存储过程被锁
A、 脚本说明:
实际过程中可能你要重新编译某个存储过程理总是处于等待状态,最后会报无法锁定对象,这时你就可以用这个脚本找到锁定过程的那个sid,需要注意的是查v$access这个视图本来就很慢,需要一些布耐心。
B、脚本原文:
SELECT * FROM V$ACCESS
WHERE OWNER=
AND OBJECT
?
查看回滚段状态
A、 脚本说明
这也是DBA经常使用的脚本,因为回滚段是online还是full是他们的关怀之列嘛
B、脚本原文:
SELECT A.SEGMENT_NAME,B.STATUS
FROM DBA_ROLLBACK_SEGS A,
V$ROLLSTAT B
WHERE A.SEGMENT_ID=B.USN
ORDER BY 2
看哪些session正在使用哪些回滚段
A、 脚本说明:
当你发现一个回滚段处理full状态,你想使它变回online状态,这时你便会用alter rollback segment rbs_seg_name shrink,可很多时侯确shrink不回来,主要是由于某个session在用,这时你就用到了这个脚本,找到了sid的serial#余下的事就不用我说了吧。
B、脚本原文
SELECT R.NAME 回滚段名,
S.SID,
S.SERIAL#,
S.USERNAME 用户名,
S.STATUS,
T.CR_GET,
T.PHY_IO,
T.USED_UBLK,
T.NOUNDO,
SUBSTR(S.PROGRAM, 1, 78) 操作程序
FROM SYS.V_$SESSION S,SYS.V_$TRANSACTION T,SYS.V_$ROLLNAME R
WHERE T.ADDR = S.TADDR AND T.XIDUSN = R.USN
-- AND R.NAME IN ('ZHYZ_RBS')
ORDER? BY T.CR_GET,T.PHY_IO
查看正在使用临时段的session
A、 脚本说明:
许多的时侯你在查看哪些段无法扩展时,回显的结果是临时段,或你做表空间统计时发现临段表空间的可用空间几乎为0,这时按oracle的说法是你只有重新启动数据库才能回收这部分空间。实际过程中没那么复杂,使用以下这段脚本把占用临时段的session杀掉,然后用alter tablespace temp coalesce;这个语句就把temp表空间的空间回收回来了。
B、 脚本原文
SELECT USERNAME,SID,SERIAL#,SQL_ADDRESS,MACHINE,PROGRAM,
TABLESPACE,SEGTYPE,CONTENTS FROM V$SESSION SE,V$SORT_USAGE SU
WHERE SE.SADDR=SU.SESSION_ADDR;
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/eastcafe/archive/2007/08/30/1765319.aspx