分类: Oracle
2009-02-23 15:32:52
数据库各个表空间增长情况的检查: SELECT SW.SID,S.USERNAME,SW.EVENT,SW.WAIT_TIME,SW.STATE,SW.SECONDS_IN_WAIT SEC_IN_WAIT SQL>SELECT SEGMENT_NAME,OWNER,TABLESPACE_NAME,INITIAL_EXTENT,NEXT_EXTENT,DBA_ROLLBACK_SEGS.STATUS FROM DBA_ROLLBACK_SEGS,V$DATAFILE WHERE FILE_ID=FILE#; 查看回滚段的使用情况,哪个用户正在使用回滚段的资源: 检查共享内存的剩余情况: 数据高速缓冲区性能检查: 数据库回滚段性能检查: 杀会话的脚本: 10、捕捉运行很久的SQL: SELECT A.USERNAME, A.MACHINE,A.PROGRAM,A.SID,A.SERIAL#,A.STATUS,C.PIECE,C.SQL_TEXT FROM V$SESSION A,V$PROCESS B,V$SQLTEXT C WHERE B.SPID=SPID AND B.ADDR=A.PADDR AND A.SQL_ADDRESS=C.ADDRESS(+) ORDER BY C.PIECE; |
根据SID找ORACLE的某个进程: SQL> SELECT PRO.SPID FROM V$SESSION SES,V$PROCESS PRO WHERE SES.SID=21 AND SES.PADDR=PRO.ADDR; 监控当前数据库谁在运行什么SQL语句: SQL>SELECT OSUSER, USERNAME, SQL_TEXT FROM V$SESSION A, V$SQLTEXT B WHERE A.SQL_ADDRESS =B.ADDRESS ORDER BY ADDRESS, PIECE; 如何查看数据库中某用户,正在运行什么SQL语句 SQL>SELECT SQL_TEXT FROM V$SQLTEXT T, V$SESSION S WHERE T.ADDRESS=S.SQL_ADDRESS AND T.HASH_VALUE=S.SQL_HASH_VALUE AND S.MACHINE='XXXXX' OR USERNAME='WACOS'; 如何查出前台正在发出的sql语句: SQL> SELECT USER_NAME,SQL_TEXT FROM V$OPEN_CURSOR WHERE SID IN(SELECT SID FROM (SELECT SID,SERIAL# FROM V$SESSION WHERE STATUS='ACTIVE')); 查询当前所执行的SQL语句: SQL> SELECT PROGRAM ,SQL_ADDRESS FROM V$SESSION WHERE PADDR IN (SELECT ADDR FROM V$PROCESS WHERE SPID=3556); PROGRAM SQL_ADDRESS ------------------------------------------------ ---------------- SQLPLUS@CTC20 (TNS V1-V3) 000000038FCB SQL> SELECT SQL_TEXT FROM V$SQLAREA WHERE ADDRESS='000000038FCB 找出消耗CPU最高的进程对应的SQL语句: SET LINE 240 SET VERIFY OFF COLUMN SID FORMAT 999 COLUMN PID FORMAT 999 COLUMN S_# FORMAT 999 COLUMN USERNAME FORMAT A9 HEADING "ORA USER" COLUMN PROGRAM FORMAT A29 COLUMN SQL FORMAT A60 COLUMN OSNAME FORMAT A9 HEADING "OS USER" SELECT P.PID PID,S.SID SID,P.SPID SPID,S.USERNAME USERNAME,S.OSUSER OSNAME,P.SERIAL# S_#,P.TERMINAL,P.PROGRAM PROGRAM,P.BACKGROUND,S.STATUS,RTRIM(SUBSTR(A.SQL_TEXT, 1, 80)) SQLFROM V$PROCESS P, V$SESSION S,V$SQLAREA A WHERE P.ADDR = S.PADDR AND S.SQL_ADDRESS = A.ADDRESS (+) AND P.SPID LIKE '%&1%'; ENTER VALUE FOR 1: PID¡(这里输入占用CPU最高的进程对应的PID) SET TERMOUT OFF SPOOL MAXCPU.TXT SELECT '++'||S.USERNAME USERNAME,RTRIM(REPLACE(A.SQL_TEXT,CHR(10),''))||';'FROM V$PROCESS P, V$SESSION S,V$SQLAREA A WHERE P.ADDR = S.PADDR AND S.SQL_ADDRESS = A.ADDRESS (+) AND P.SPID LIKE '%&&1%'; Enter value for 1: PID(这里输入占用CPU最高的进程对应的PID) spool off(这句放在最后执行) CPU用率最高的2条SQL语句的获取
SELECT SQL_TEXT FROM V$SQL WHERE ADDRESS IN (SELECT SQL_ADDRESS FROM V$SESSION WHERE SID IN (SELECT SID FROM V$SESSION WHERE PADDR IN (SELECT ADDR FROM V$PROCESS WHERE SPID=&PID))); |
25、查询有哪些数据库实例在运行:
select inst_name from v$active_instances;
26、以DBA角色, 查看当前数据库里锁的情况:
SELECT OBJECT_ID,SESSION_ID,LOCKED_MODE FROM V$LOCKED_OBJECT;
SELECT T2.USERNAME,T2.SID,T2.SERIAL#,T2.LOGON_TIME FROM V$LOCKED_OBJECT T1,V$SESSION T2 WHERE T1.SESSION_ID=T2.SID ORDER BY T2.LOGON_TIME;
27、查看表是否是分区表:
SELECT TABLE_NAME,PARTITIONED FROM USER_TABLES WHERE TABLE_NAME='LOCALUSAGE';
TABLE_NAME PAR
------------------------------ --- ---------
LOCALUSAGE YES
28、查看分区表的分区名和相应的表空间名:
SELECT TABLE_NAME, PARTITION_NAME,TABLESPACE_NAME FROM USER_TAB_PARTITIONS WHERE TABLE_NAME LIKE '%USAGE%';
29、查看索引是否是分区索引:
SELECT INDEX_NAME, TABLE_NAME, STATUS, PARTITIONED FROM USER_INDEXES WHERE TABLE_NAME LIKE '%USAGE';
30、如果返回的PATITIONED为YES,请再执行如下语句来查询分区索引的类型:
SELECT INDEX_NAME,TABLE_NAME,LOCALITY FROM USER_PART_INDEXES;
31、Dual是Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select中.
查看系统时间:
SELECT TO_CHAR(SYSDATE,'YY-MM-DD HH24:MI:SS') SHIJIAN FROM DUAL;
32、查看索引段中extent的数量:
SELECT SEGMENT_NAME,COUNT(*) FROM DBA_EXTENTS
WHERE SEGMENT_TYPE='INDEX' AND OWNER='SCOTT' GROUP BY SEGMENT_NAME;
33、查看系统表中的用户索引(用来检查在system表空间内其他用户索引的存在):
SQL>SELECT COUNT(*) FROM DBA_INDEXES WHERE TABLESPACE_NAME='SYSTEM' AND OWNER NOT IN('SYS','SYSTEM');
34、查看wacos表空间内的索引的扩展情况:
SELECT SUBSTR(SEGMENT_NAME,1,20) "SEGMENT NAME",BYTES, COUNT(BYTES)
FROM DBA_EXTENTS WHERE SEGMENT_NAME IN( SELECT INDEX_NAME FROM DBA_INDEXES
WHERE TABLESPACE_NAME = 'WACOS') GROUP BY SEGMENT_NAME,BYTES ORDER BY SEGMENT_NAME;
35、查看表空间数据文件的读写性能:
SQL>SELECT NAME,PHYRDS,PHYWRTS,AVGIOTIM,MINIOTIM,MAXIOWTM,MAXIORTM FROM V$FILESTAT,V$DATAFILE WHERE V$FILESTAT.FILE#=V$DATAFILE.FILE#;
SQL>SELECT FS.NAME NAME,F.PHYRDS,F.PHYBLKRD,F.PHYWRTS,F.PHYBLKWRT ,F.READTIM,F.WRITETIM
FROM V$FILESTAT F, V$DATAFILE FS WHERE F.FILE# = FS.FILE# ORDER BY FS.NAME;
(注意:如果phyblkrd与phyrds很接近的话,则表明这个表空间中存在全表扫描的表,这些表需要调整索引或优化SQL语句)
36、转换表空间为local方式管理:
SQL> EXEC SYS.DBMS_SPACE_ADMIN.TABLESPACE_MIGRATE_TO_LOCAL('TBS_TEST') ;
37、查看一下哪个用户在用临时段:
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;
38、查看占io较大的正在运行的session:
SELECT SE.SID,SE.SERIAL#,PR.SPID,SE.USERNAME,SE.STATUS,SE.TERMINAL,SE.PROGRAM, SE.MODULE,SE.SQL_ADDRESS,ST.EVENT,ST.P1TEXT,SI.PHYSICAL_READS,SI.BLOCK_CHANGES FROM V$SESSION SE,V$
39、查找前十条性能差的sql:
SELECT * FROM(SELECT PARSING_USER_ID EXECUTIONS,SORTS,COMMAND_TYPE,DISK_READS,SQL_TEXT FROM V$SQLAREA ORDER BY DISK_READS DESC) WHERE ROWNUM<10;
40、删除用户下所有表的语句:
SELECT 'DROP TABLE '||TABLE_NAME||' CASCADE CONSTRAINTS;' FROM USER_TABLES;
41、查看LOCK,并杀掉会话:
SET LINESIZE 132 PAGESIZE 66
BREAK ON KILL ON USERNAME ON TERMINAL
COLUMN KILL HEADING 'KILL STRING' FORMAT A13
COLUMN RES HEADING 'RESOURCE TYPE' FORMAT 999
COLUMN ID1 FORMAT 9999990
COLUMN ID2 FORMAT 9999990
COLUMN LMODE BEADING 'LOCK HELD' FORMAT A20
COLUMN REQUEST HEADING 'LOCK REQUESTED' FORMAT A20
COLUMN SERIAL# FORMAT 99999
COLUMN USERNAME FORMAT A10 HEADING "USERNAME"
COLUMN TERMINAL HEADING TERM FORMAT A6
COLUMN TAB FORMAT A35 HEADING "TABLE NAME"
COLUMN OWNER FORMAT A9
COLUMN ADDRESS FORMAT A18
SELECT NVL(S.USERNAME, 'INTERNAL') USERNAME,
NVL(S.TERMINAL, 'NONE') TERMINAL,
L.SID || ',' || S.SERIAL# KILL,
U1.NAME || ',' || SUBSTR(T1.NAME, 1, 20) TAB,
DECODE(L.LMODE,
1,'NO LOCK',
2,'ROW SHARE',
3,'ROW EXCLUSIVE',
4,'SHARE',
5,'SHARE ROW EXCLUSIVE',
6,'EXCLUSIVE',
NULL) LMODE,
DECODE(L.REQUEST,
1,'NO LOCK',
2,'ROW SHARE',
3,'ROW EXCLUSIVE',
4,'SHARE',
5,'SHARE ROW EXCLUSIVE',
6,'EXCLUSIVE',
NULL) REQUEST
FROM V$LOCK L, V$SESSION S, SYS.USER$ U1, SYS.OBJ$ T1
WHERE L.SID = S.SID
AND T1.OBJ# = DECODE(L.ID2, 0, L.ID1, L.ID2)
AND U1.USER# = T1.OWNER#
AND S.TYPE != 'BACKGROUND'
ORDER BY 1, 2, 5;
--ALTER SYSTEM KILL SESSION ' , ';
COLUMN USERNAME FORMAT A15
COLUMN SID FORMAT 9990 HEADING SID
COLUMN TYPE FORMAT A4
COLUMN LMODE FORMAT 990 HEADING 'HELD'
COLUMN REQUEST FORMAT 990 HEADING 'REQ'
COLUMN ID1 FORMAT 9999990
COLUMN ID2 FORMAT 9999990
BREAK ON ID1 SKIP 1 DUP
SPOOL TFSLCKWT.LST
SELECT SN.USERNAME,M.SID,M.TYPE,
DECODE(M.LMODE,
0,'NONE',
1,'NULL',
2,'ROW SHARE',
3,'ROW EXCL.',
4,'SHARE',
5,'S/ROW EXCL.',
6,'EXCLUSIVE',
LMODE,
LTRIM(TO_CHAR(LMODE, '990'))) LMODE,
DECODE(M.REQUEST,
0,'NONE',
1,'NULL',
2,'ROW SHARE',
3,'ROW EXCL.',
4,'SHARE',
5,'S/ROW EXCL.',
6,'EXCLUSIVE',
REQUEST,LTRIM(TO_CHAR(M.REQUEST, '990'))) REQUEST,
M.ID1,M.ID2
FROM V$SESSION SN, V$LOCK M
WHERE (SN.SID = M.SID AND M.REQUEST != 0)
OR (SN.SID = M.SID AND M.REQUEST = 0 AND LMODE != 4 AND
(ID1, ID2) IN (SELECT S.ID1, S.ID2
FROM V$LOCK S
WHERE REQUEST != 0
AND S.ID1 = M.ID1 AND S.ID2 = M.ID2))
ORDER BY ID1, ID2, M.REQUEST;
SPOOL OFF
CLEAR BREAKS