SQL語句大全—檢視錶空間(二)

polalisi發表於2007-10-16

資料庫各個表空間增長情況的檢查:
SQL>SELECT A.TABLESPACE_NAME,(
1-(A.TOTAL)/B.TOTAL)*100 USED_PERCENT
FROM (SELECT TABLESPACE_NAME,SUM(BYTES) TOTAL FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME) A,(SELECT TABLESPACE_NAME,SUM(BYTES) TOTAL FROM DBA_DATA_FILES GROUP BY TABLESPACE_NAME) B WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME;

SQL>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;
檢視各個表空間佔用磁碟情況:
SQL>COL TABLESPACE_NAME FORMAT A20;
SQL>SELECT B.FILE_ID FILE_ID,
B.TABLESPACE_NAME TABLESPACE_NAME,
B.BYTES BYTES,
(B.BYTES-SUM(NVL(A.BYTES,
0))) USED,
SUM(NVL(A.BYTES,
0)) FREE,
SUM(NVL(A.BYTES,
0))/(B.BYTES)*100 PERCENT
FROM DBA_FREE_SPACE A,DBA_DATA_FILES B
WHERE A.FILE_ID=B.FILE_ID
GROUP BY B.TABLESPACE_NAME,B.FILE_ID,B.BYTES
ORDER BY B.FILE_ID;
資料庫物件下一擴充套件與表空間的free擴充套件值的檢查:
SQL>SELECT A.TABLE_NAME, A.NEXT_EXTENT, A.TABLESPACE_NAME
FROM ALL_TABLES A,(SELECT TABLESPACE_NAME, MAX(BYTES) AS BIG_CHUNK
FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME ) F WHERE F.TABLESPACE_NAME = A.TABLESPACE_NAME AND A.NEXT_EXTENT > F.BIG_CHUNK
UNION SELECT A.INDEX_NAME, A.NEXT_EXTENT, A.TABLESPACE_NAME
FROM ALL_INDEXES A,(SELECT TABLESPACE_NAME, MAX(BYTES) AS BIG_CHUNK
FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME ) F WHERE F.TABLESPACE_NAME = A.TABLESPACE_NAME AND A.NEXT_EXTENT > F.BIG_CHUNK;
Disk Read最高的SQL語句的獲取:
SQL>SELECT SQL_TEXT FROM (SELECT * FROM V$SQLAREA ORDER BY DISK_READS)
WHERE ROWNUM<=
5;
查詢前十條效能差的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 ;
等待時間最多的5個系統等待事件的獲取:
SQL>SELECT * FROM (SELECT * FROM V$SYSTEM_EVENT WHERE EVENT NOT LIKE
'SQL%' ORDER BY TOTAL_WAITS DESC) WHERE ROWNUM<=5;
檢視當前等待事件的會話:
COL USERNAME FORMAT A10
SET LINE
120
COL EVENT FORMAT A30
SELECT SE.SID,S.USERNAME,SE.EVENT,SE.TOTAL_WAITS,SE.TIME_WAITED,SE.AVERAGE_WAIT
FROM V$SESSION S,V$SESSION_EVENT SE WHERE S.USERNAME IS NOT NULL AND SE.SID=S.SID
AND S.STATUS=
'ACTIVE' AND SE.EVENT NOT LIKE '%SQL*NET%';

SELECT SID, EVENT, P1, P2, P3, WAIT_TIME, SECONDS_IN_WAIT, STATE FROM V$SESSION_WAIT WHERE EVENT NOT LIKE
'%MESSAGE%' AND EVENT NOT LIKE 'SQL*NET%' AND EVENT NOT LIKE '%TIMER%' AND EVENT != 'WAKEUP TIME MANAGER';
找到與所連線的會話有關的當前等待事件:

SELECT SW.SID,S.USERNAME,SW.EVENT,SW.WAIT_TIME,SW.STATE,SW.SECONDS_IN_WAIT SEC_IN_WAIT
FROM V$SESSION S,V$SESSION_WAIT SW WHERE S.USERNAME IS NOT NULL AND SW.SID=S.SID
AND SW.EVENT NOT LIKE
'%SQL*NET%' ORDER BY SW.WAIT_TIME DESC;
Oracle所有回滾段狀態的檢查:

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#;
Oracle回滾段擴充套件資訊的檢查:
COL NAME FORMAT A10
SET LINESIZE
140
SELECT SUBSTR(NAME,
1,40) NAME,EXTENTS,RSSIZE,OPTSIZE,AVEACTIVE,EXTENDS,WRAPS,SHRINKS,HWMSIZE
FROM V$ROLLNAME RN,V$ROLLSTAT RS WHERE (RN.USN=RS.USN);

EXTENTS
:回滾段中的盤區數量。Rssize:以位元組為單位的回滾段的尺寸。optsize:為optimal引數設定的值。Aveactive:從回滾段中刪除盤區時釋放的以位元組為單位的平均空間的大小。Extends:系統為回滾段增加的盤區的次數。Shrinks:系統從回滾段中清除盤區(即回滾段收縮)的次數。回滾段每次清除盤區時,系統可能會從這個回滾段中消除一個或多個盤區。Hwmsize:回滾段尺寸的上限,即回滾段曾經達到的最大尺寸。(如果回滾段平均尺寸接近OPTIMAL的值,那麼說明OPTIMAL的值設定正確,如果回滾段動態增長次數或收縮次數很高,那麼需要提高OPTIMAL的值)

檢視回滾段的使用情況,哪個使用者正在使用回滾段的資源:
SELECT S.USERNAME, U.NAME FROM V$TRANSACTION T,V$ROLLSTAT R,
V$ROLLNAME U,V$SESSION S WHERE S.TADDR=T.ADDR AND
T.XIDUSN=R.USN AND R.USN=U.USN ORDER BY S.USERNAME;
如何檢視一下某個shared_server正在忙什麼:
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=
13161 AND B.ADDR=A.PADDR
AND A.SQL_ADDRESS=C.ADDRESS(+) ORDER BY C.PIECE;
資料庫共享池效能檢查:
SELECT NAMESPACE,GETS,GETHITRATIO,PINS,PINHITRATIO,RELOADS,INVALIDATIONS FROM V$LIBRARYCACHE WHERE NAMESPACE IN(
'SQLAREA','TABLE/PROCEDURE','BODY','TRIGGER');
檢查資料過載比率:
SELECT SUM(RELOADS)/SUM(PINS)*
100 "RELOAD RATIO" FROM
V$LIBRARYCACHE;
檢查資料字典的命中率:
SELECT
1-SUM(GETMISSES)/SUM(GETS) "DATA DICTIONARY HIT
RATIO" FROM V$ROWCACHE;
(對於library cache, gethitratiopinhitratio應該大於90%,對於資料過載比率,reload ratio應該小於1%,對於資料字典的命中率,data dictionary hit ratio應該大於85%)

檢查共享記憶體的剩餘情況:
SELECT REQUEST_MISSES, REQUEST_FAILURES FROM V$SHARED_POOL_RESERVED;
(對於共享記憶體的剩餘情況, request_misses request_failures應該接近0)

資料高速緩衝區效能檢查:
SELECT
1-P.VALUE/(B.VALUE+C.VALUE) "DB BUFFER CACHE HIT RATIO" FROM V$SYSSTAT P,V$SYSSTAT B,V$SYSSTAT C WHERE P.NAME='PHYSICAL READS' AND B.NAME='DB BLOCK GETS' AND C.NAME='CONSISTENT GETS';
檢查buffer pool HIT_RATIO執行
SELECT NAME, (PHYSICAL_READS/(DB_BLOCK_GETS+CONSISTENT_GETS)) "MISS_HIT_RATIO" FROM V$BUFFER_POOL_STATISTICS WHERE (DB_BLOCK_GETS+ CONSISTENT_GETS)>
0;(正常時db buffer cache hit ratio 應該大於90%,正常時buffer pool MISS_HIT_RATIO 應該小於10%)

資料庫回滾段效能檢查:檢查Ratio執行
SELECT SUM(WAITS)*
100 /SUM(GETS) "RATIO", SUM(WAITS) "WAITS", SUM(GETS) "GETS" FROM V$ROLLSTAT;
檢查count/value執行:
SELECT CLASS,COUNT FROM V$WAITSTAT WHERE CLASS LIKE
'%UNDO%';
SELECT VALUE FROM V$SYSSTAT WHERE NAME=
'CONSISTENT GETS';(兩者的value值相除)
檢查average_wait執行:
SELECT EVENT,TOTAL_WAITS,TIME_WAITED,AVERAGE_WAIT FROM V$SYSTEM_EVENT WHERE EVENT LIKE
'%UNDO%';
檢查RBS header get ratio執行:
SELECT N.NAME,S.USN,S.WRAPS, DECODE(S.WAITS,
0,1,1- S.WAITS/S.GETS)"RBS HEADER GET RATIO" FROM V$ROLLSTAT S,V$ROLLNAME N WHERE S.USN=N.USN;(正常時Ratio應該小於1%, count/value應該小於0.01%,average_wait最好為0,該值越小越好,RBS header get ratio應該大於95%)

殺會話的指令碼:
SELECT A.SID,B.SPID,A.SERIAL#,A.LOCKWAIT,A.USERNAME,A.OSUSER,A.LOGON_TIME,A.LAST_CALL_ET/
3600 LAST_HOUR,A.STATUS, 'ORAKILL '||SID||' '||SPID HOST_COMMAND,'ALTER SYSTEM KILL SESSION '''||A.SID||','||A.SERIAL#||'''' SQL_COMMAND FROM V$SESSION A,V$PROCESS B WHERE A.PADDR=B.ADDR AND SID>6;
檢視排序段的效能:
SQL>SELECT NAME, VALUE FROM V$SYSSTAT WHERE NAME IN (
'SORTS (MEMORY)', 'SORTS (DISK)');
7、檢視資料庫庫物件:
SELECT OWNER, OBJECT_TYPE, STATUS, COUNT(*) COUNT# FROM ALL_OBJECTS GROUP BY OWNER, OBJECT_TYPE, STATUS;
8、檢視資料庫的版本: 
SELECT * FROM V$VERSION;
9、檢視資料庫的建立日期和歸檔方式:
SELECT CREATED, LOG_MODE, LOG_MODE FROM V$DATABASE;

10、捕捉執行很久的SQL:
COLUMN USERNAME FORMAT A12
COLUMN OPNAME FORMAT A16
COLUMN PROGRESS FORMAT A8
SELECT USERNAME,SID,OPNAME,ROUND(SOFAR*
100 / TOTALWORK,0) || '%' AS PROGRESS,TIME_REMAINING,SQL_TEXT FROM V$SESSION_LONGOPS , V$SQL WHERE TIME_REMAINING <> 0 AND SQL_ADDRESS=ADDRESS AND SQL_HASH_VALUE = HASH_VALUE;
11、檢視資料表的引數資訊:
SELECT PARTITION_NAME, HIGH_VALUE, HIGH_VALUE_LENGTH, TABLESPACE_NAME,PCT_FREE, PCT_USED, INI_TRANS, MAX_TRANS, INITIAL_EXTENT,NEXT_EXTENT, MIN_EXTENT, MAX_EXTENT, PCT_INCREASE, FREELISTS,FREELIST_GROUPS, LOGGING, BUFFER_POOL, NUM_ROWS, BLOCKS,EMPTY_BLOCKS, AVG_SPACE, CHAIN_CNT, AVG_ROW_LEN, SAMPLE_SIZE,LAST_ANALYZED FROM DBA_TAB_PARTITIONS
--WHERE TABLE_NAME = :TNAME AND TABLE_OWNER = :TOWNER
ORDER BY PARTITION_POSITION;
12、檢視還沒提交的事務:
SELECT * FROM V$LOCKED_OBJECT;
SELECT * FROM V$TRANSACTION;
13、查詢object為哪些程式所用:
SELECT P.SPID,S.SID,S.SERIAL# SERIAL_NUM,S.USERNAME USER_NAME,
A.TYPE OBJECT_TYPE,S.OSUSER OS_USER_NAME,A.OWNER,A.OBJECT OBJECT_NAME,DECODE(SIGN(
48 - COMMAND),1,
TO_CHAR(COMMAND),
'ACTION CODE #' || TO_CHAR(COMMAND) ) ACTION,
P.PROGRAM ORACLE_PROCESS,S.TERMINAL TERMINAL,S.PROGRAM PROGRAM,S.STATUS SESSION_STATUS FROM V$SESSION S, V$ACCESS A, V$PROCESS P WHERE S.PADDR = P.ADDR AND S.TYPE =
'USER' AND A.SID = S.SID AND A.OBJECT='SUBSCRIBER_ATTR'ORDER BY S.USERNAME, S.OSUSER;
14、檢視回滾段:
SQL>COL NAME FORMAT A10
SQL>SET LINESIZE
100
SQL>SELECT ROWNUM, SYS.DBA_ROLLBACK_SEGS.SEGMENT_NAME NAME, V$ROLLSTAT.EXTENTS EXTENTS, V$ROLLSTAT.RSSIZE SIZE_IN_BYTES, V$ROLLSTAT.XACTS XACTS, V$ROLLSTAT.GETS GETS, V$ROLLSTAT.WAITS WAITS, V$ROLLSTAT.WRITES WRITES, SYS.DBA_ROLLBACK_SEGS.STATUS STATUS FROM V$ROLLSTAT, SYS.DBA_ROLLBACK_SEGS, V$ROLLNAME WHERE V$ROLLNAME.NAME(+) = SYS.DBA_ROLLBACK_SEGS.SEGMENT_NAME AND V$ROLLSTAT.USN (+) = V$ROLLNAME.USN ORDER BY ROWNUM;
15、耗資源的程式(top session):
SELECT S.SCHEMANAME SCHEMA_NAME,DECODE(SIGN(
48 - COMMAND), 1, TO_CHAR(COMMAND), 'ACTION CODE #' || TO_CHAR(COMMAND) ) ACTION,STATUS SESSION_STATUS,S.OSUSER OS_USER_NAME,S.SID,P.SPID,S.SERIAL# SERIAL_NUM,NVL(S.USERNAME,'[ORACLE PROCESS]') USER_NAME,S.TERMINAL TERMINAL,S.PROGRAM PROGRAM,ST.VALUE CRITERIA_VALUE FROM V$SESSTAT ST,V$SESSION S,V$PROCESS P WHERE ST.SID = S.SID AND ST.STATISTIC# = TO_NUMBER('38') AND ('ALL'='ALL' OR S.STATUS ='ALL') AND P.ADDR=S.PADDR ORDER BY ST.VALUE DESC,P.SPID ASC,S.USERNAME ASC,S.OSUSER ASC;
根據PID查詢相應的語句:

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;

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10455649/viewspace-976950/,如需轉載,請註明出處,否則將追究法律責任。

相關文章