`
fantaxy025025
  • 浏览: 1309076 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Oracle的SQL语句大全(摘)

阅读更多
SQL语句大全—查看表空间
 

数据库各个表空间增长情况的检查:
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, gethitratio pinhitratio 应该大于 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;

 
 
SQL语句大全—查看数据(三)
2007年08月16日 星期四 上午 02:17
根据 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)                        
000000038 FCB1A90
SQL > SELECT SQL_TEXT FROM V$SQLAREA WHERE ADDRESS=
'000000038FCB1A90'
;

找出消耗 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 语句的获取
执行: top ,通过 top 获得 CPU 占用率最高的进程的 pid
SQL >SELECT SQL_TEXT,SPID,V$SESSION.PROGRAM,PROCESS FROM V$SQLAREA,V$SESSION,V$PROCESS WHERE V$SQLAREA.ADDRESS=V$SESSION.SQL_ADDRESS AND V$SQLAREA.HASH_VALUE=V$SESSION.SQL_HASH_VALUE AND V$SESSION.PADDR=V$PROCESS.ADDR AND V$PROCESS.SPID IN (PID);
COL MACHINE FORMAT A30
COL PROGRAM FORMAT A40
SET LINE
200

SQL >SELECT SID,SERIAL# ,USERNAME,OSUSER,MACHINE,PROGRAM,PROCESS,TO_CHAR(LOGON_TIME,
'YYYY/MM/DD HH24:MI:SS'
) FROM V$SESSION WHERE PADDR IN (SELECT ADDR FROM V$PROCESS WHERE SPID IN ([$SPID]));

SELECT SQL_TEXT FROM V$SQLTEXT_WITH_NEWLINES
WHERE HASH_VALUE=(SELECT SQL_HASH_VALUE FROM V$SESSION WHERE SID=&SID)
ORDER BY PIECE;

16 、查看锁 lock 情况 :

SQL >SELECT
/*+ RULE */
LS.OSUSER OS_USER_NAME,
LS.USERNAME USER_NAME,
DECODE(LS.TYPE ,
        
'RW' , 'ROW WAIT ENQUEUE LOCK' ,
        
'TM' , 'DML ENQUEUE LOCK'
,
        
'TX' , 'TRANSACTION ENQUEUE LOCK'
,
        
'UL' , 'USER SUPPLIED LOCK'
) LOCK_TYPE,
O.OBJECT_NAME OBJECT ,
DECODE(LS.LMODE,
        
1
,NULL ,
        
2 , 'ROW SHARE'
,
        
3 , 'ROW EXCLUSIVE'
,
        
4 , 'SHARE'
,
        
5 , 'SHARE ROW EXCLUSIVE'
,
        
6 , 'EXCLUSIVE'
,
         NULL ) LOCK_MODE,
O.OWNER,
LS.SID,
LS.SERIAL# SERIAL_NUM,
LS.ID1,
LS.ID2
FROM SYS .DBA_OBJECTS O,
        (SELECT S.OSUSER,
                S.USERNAME,
                L.TYPE ,
                L.LMODE,
                S.SID,
                S.SERIAL#,
                L.ID1,
                L.ID2
           FROM V$SESSION S, V$LOCK L
          WHERE S.SID = L.SID) LS
WHERE O.OBJECT_ID = LS.ID1
    AND O.OWNER <>
'SYS'

ORDER BY O.OWNER, O.OBJECT_NAME;

SQL >SELECT SYS .V_$SESSION.OSUSER,
        SYS .V_$SESSION.MACHINE,
        V$LOCK.SID,
        SYS .V_$SESSION.SERIAL#,
        DECODE(V$LOCK.TYPE ,
              
'MR' , 'MEDIA RECOVERY' ,
              
'RT' , 'REDO THREAD'
,
              
'UN' , 'USER NAME'
,
              
'TX' , 'TRANSACTION'
,
              
'TM' , 'DML'
,
              
'UL' , 'PL/SQL USER LOCK'
,
              
'DX' , 'DISTRIBUTED XACTION'
,
              
'CF' , 'CONTROL FILE'
,
              
'IS' , 'INSTANCE STATE'
,
              
'FS' , 'FILE SET'
,
              
'IR' , 'INSTANCE RECOVERY'
,
              
'ST' , 'DISK SPACE TRANSACTION'
,
              
'TS' , 'TEMP SEGMENT'
,
              
'IV' , 'LIBRARY CACHE INVALIDA-TION'
,
              
'LS' , 'LOG START OR SWITCH'
,
              
'RW' , 'ROW WAIT'
,
              
'SQ' , 'SEQUENCE NUMBER'
,
              
'TE' , 'EXTEND TABLE'
,
              
'TT' , 'TEMP TABLE'
,
              
'UNKNOWN'
) LOCKTYPE,
        RTRIM(OBJECT_TYPE) ||
' ' || RTRIM(OWNER) || '.'
|| OBJECT_NAME OBJECT_NAME,
        DECODE(LMODE,
              
0 , 'NONE'
,
              
1 , 'NULL'
,
              
2 , 'ROW-S'
,
              
3 , 'ROW-X'
,
              
4 , 'SHARE'
,
              
5 , 'S/ROW-X'
,
              
6 , 'EXCLUSIVE'
,
              
'UNKNOWN'
) LOCKMODE,
        DECODE(REQUEST,
              
0 , 'NONE'
,
              
1 , 'NULL'
,
              
2 , 'ROW-S'
,
              
3 , 'ROW-X'
,
              
4 , 'SHARE'
,
              
5 , 'S/ROW-X'
,
              
6 , 'EXCLUSIVE'
,
               
'UNKNOWN'
) REQUESTMODE,
        CTIME,
        BLOCK B
FROM V$LOCK, ALL_OBJECTS, SYS .V_$SESSION
WHERE V$LOCK.SID >
6

    AND SYS .V_$SESSION.SID = V$LOCK.SID
    AND V$LOCK.ID1 = ALL_OBJECTS.OBJECT_ID;

DBA 角色 , 查看当前数据库里锁的情况可以用如下 SQL 语句:
COL OWNER FOR A12
COL OBJECT_NAME FOR A16
SELECT B.OWNER,B.OBJECT_NAME,L.SESSION_ID,L.LOCKED_MODE
FROM V$LOCKED_OBJECT L, DBA_OBJECTS B
WHERE B.OBJECT_ID=L.OBJECT_ID;

SQL >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;


SQL >SELECT SQL_ADDRESS FROM V$SESSION WHERE SID=;


SQL >SELECT * FROM V$SQLTEXT WHERE ADDRESS=;


SQL >SELECT COMMAND_TYPE,PIECE,SQL_TEXT FROM V$SQLTEXT WHERE ADDRESS=(SELECT SQL_ADDRESS FROM V$SESSION A WHERE SID=
18 );    


SQL >SELECT OBJECT_ID FROM V$LOCKED_OBJECT;


SQL >SELECT OBJECT_NAME,OBJECT_TYPE FROM DBA_OBJECTS WHERE OBJECT_ID=
'' ;
如果有长期出现的一列,可能是没有释放的锁。我们可以用下面 SQL 语句杀掉长期没有释放非正常的锁: SQL >ALTER SYSTEM KILL SESSION 'SID,SERIAL#'
;

17 、查看等待( wait )情况 :

SQL >SELECT V$WAITSTAT.CLASS ,V$WAITSTAT.COUNT COUNT , SUM (V$SYSSTAT.VALUE ) SUM_VALUE FROM V$WAITSTAT,V$SYSSTAT WHERE V$SYSSTAT.NAME IN (
'DB BLOCK GETS' , 'CONSISTENT GETS' ) GROUP BY V$WAITSTAT.CLASS ,V$WAITSTAT.COUNT ;

18 、查看 sga 情况 :

SQL >SELECT NAME , BYTES FROM SYS .V_$SGASTAT ORDER BY NAME ASC ;

19 、查看 catched object:
SQL >SELECT OWNER,NAME ,DB_LINK,NAMESPACE,TYPE ,SHARABLE_MEM,LOADS, EXECUTIONS,LOCKS,PINS,KEPT FROM V$DB_OBJECT_CACHE;

20 、查看 V$SQLAREA:
SQL >SELECT SQL_TEXT,SHARABLE_MEM,PERSISTENT_MEM,RUNTIME_MEM,SORTS,
VERSION_COUNT,LOADED_VERSIONS,OPEN_VERSIONS,USERS_OPENING,EXECUTIONS,
USERS_EXECUTING,LOADS,FIRST_LOAD_TIME,INVALIDATIONS,PARSE_CALLS,
DISK_READS,BUFFER_GETS,ROWS_PROCESSED FROM V$SQLAREA;

21 、查看 object 分类数量 :
SELECT DECODE(O.TYPE #,
1 , 'INDEX' , 2 , 'TABLE' , 3 , 'CLUSTER' , 4 , 'VIEW' , 5 , 'SYNONYM' , 6 , 'SEQUENCE' , 'OTHER' ) OBJECT_TYPE , COUNT (*) QUANTITY FROM SYS .OBJ$ O WHERE O.TYPE # > 1 GROUP BY DECODE(O.TYPE #, 1 , 'INDEX' , 2 , 'TABLE' , 3 , 'CLUSTER' , 4 , 'VIEW' , 5 , 'SYNONYM' , 6 , 'SEQUENCE' , 'OTHER' ) UNION SELECT 'COLUMN' , COUNT (*) FROM SYS .COL$ UNION SELECT 'DB LINK' , COUNT (*) FROM ALL_OBJECTS;

22 、有关 connection 的相关信息 :

1 )查看有哪些用户连接
SELECT S.OSUSER OS_USER_NAME,DECODE(SIGN(
48 - COMMAND), 1 ,TO_CHAR(COMMAND),
'ACTION CODE #' || TO_CHAR(COMMAND))ACTION,P.PROGRAM ORACLE_PROCESS, STATUS SESSION_STATUS,S.TERMINAL TERMINAL,S.PROGRAM PROGRAM, S.USERNAME USER_NAME,S.FIXED_TABLE_SEQUENCE ACTIVITY_METER, '' QUERY , 0 MEMORY, 0 MAX_MEMORY, 0 CPU_USAGE,S.SID,S.SERIAL# SERIAL_NUM FROM V$SESSION S,V$PROCESS P WHERE S.PADDR=P.ADDR AND S.TYPE = 'USER'
ORDER BY S.USERNAME, S.OSUSER;

2 )根据 v.sid 查看对应连接的资源占用等情况

SELECT N.NAME ,V.VALUE ,N.CLASS ,N.STATISTIC# FROM V$STATNAME N,V$SESSTAT V WHERE V.SID=
18 AND V.STATISTIC# = N.STATISTIC# ORDER BY N.CLASS , N.STATISTIC#;

3 )根据 sid 查看对应连接正在运行的
sql
SELECT /*+ PUSH_SUBQ */ COMMAND_TYPE,SQL_TEXT,SHARABLE_MEM, PERSISTENT_MEM,RUNTIME_MEM,SORTS,VERSION_COUNT,LOADED_VERSIONS,OPEN_VERSIONS,USERS_OPENING,EXECUTIONS, USERS_EXECUTING,LOADS,FIRST_LOAD_TIME,INVALIDATIONS,PARSE_CALLS,DISK_READS,BUFFER_GETS,ROWS_PROCESSED,SYSDATE START_TIME,SYSDATE FINISH_TIME, '>' || ADDRESS SQL_ADDRESS, 'N' STATUS FROM V$SQLAREA WHERE ADDRESS = (SELECT SQL_ADDRESS FROM V$SESSION WHERE SID= 8
);

根据 pid 查看 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)));

 
 
 
23、查询表空间使用情况:
SELECT A.TABLESPACE_NAME "
空间名称 ", 100 -ROUND((NVL(B.BYTES_FREE, 0 )/A.BYTES_ALLOC)* 100 , 2 ) " 占用率 (%) ", ROUND(A.BYTES_ALLOC/ 1024 / 1024 , 2 ) " 容量 (M) ",
ROUND(NVL(B.BYTES_FREE,
0 )/ 1024 / 1024 , 2 ) 空闲 (M)
",
ROUND((A.BYTES_ALLOC-NVL(B.BYTES_FREE,
0 ))/ 1024 / 1024 , 2 ) " 使用 (M)
",
LARGEST "
最大扩展段 (M) ",TO_CHAR(SYSDATE , 'YYYY-MM-DD HH24:MI:SS' ) " 采样时间 " FROM (SELECT F.TABLESPACE_NAME,SUM (F.BYTES) BYTES_ALLOC,SUM (DECODE(F.AUTOEXTENSIBLE, 'YES' ,F.MAXBYTES, 'NO' ,F.BYTES)) MAXBYTES FROM DBA_DATA_FILES F GROUP BY TABLESPACE_NAME) A,(SELECT F.TABLESPACE_NAME,SUM (F.BYTES) BYTES_FREE FROM DBA_FREE_SPACE F GROUP BY TABLESPACE_NAME) B,(SELECT ROUND(MAX (FF.LENGTH)* 16 / 1024 , 2
) LARGEST,TS.NAME TABLESPACE_NAME FROM SYS .FET$ FF, SYS .FILE $ TF,SYS .TS$ TS WHERE TS.TS#=FF.TS# AND FF.FILE #=TF.RELFILE# AND TS.TS#=TF.TS# GROUP BY TS.NAME , TF.BLOCKS) C WHERE A.TABLESPACE_NAME = B.TABLESPACE_NAME AND A.TABLESPACE_NAME = C.TABLESPACE_NAME;

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 ;

24 、查询表空间的碎片程度 :

SQL >SELECT TABLESPACE_NAME,COUNT (TABLESPACE_NAME) FROM DBA_FREE_SPACE GROUP BY</s
分享到:
评论

相关推荐

    Oracle sql语句大全

    Oracle sql语句大全. sql语句的优化资料

    Oracle Sql语句转换成Mysql Sql语句

    本项目提供了一个Java源码工具,能够帮助用户便捷地将Oracle SQL语句转换为MySQL SQL语句。 Oracle SQL与MySQL SQL的主要差异在于以下几个方面: 1. **数据类型**:Oracle支持的数据类型如NUMBER、LONG、RAW等在...

    ORACLE常用SQL语句大全.pdf

    Oracle 常用 SQL 语句大全 本文档总结了 Oracle 中常用的 SQL 语句,包括数据库的创建、删除、备份、表的创建、删除、修改、索引的创建、视图的创建等基本操作,以及一些高级查询运算符的使用。 一、数据库操作 ...

    oracle常用SQL语句(汇总版).docx

    Oracle 常用 SQL 语句汇总 Oracle 是一个功能强大且复杂的关系数据库管理系统,它提供了多种 SQL 语句来管理和操作数据库。在本文中,我们将详细介绍 Oracle 中常用的 SQL 语句,包括数据控制语句(DML)、数据定义...

    oracle Sql语句二

    oracle Sql语句二 oracle Sql语句二

    压测Oracle的SQL语句的性能情况

    本文将深入探讨如何利用压力测试工具来评估和优化Oracle数据库中的SQL语句性能。 标题"压测Oracle的SQL语句的性能情况"暗示了我们关注的是在高负载情况下,Oracle数据库处理SQL查询的能力。压力测试(Pressure ...

    Oracle Sql 语句大全 培训内部资料

    这份"Oracle Sql 语句大全 培训内部资料"显然包含了广泛的信息,旨在帮助用户掌握和提升在Oracle环境中使用SQL的能力。以下是根据标题、描述和标签提炼的一些关键知识点: 1. **SQL基础**:SQL(Structured Query ...

    oracle SQL语句大全

    oracle SQL语句大全

    Oracle-Sql语句资料oracle+110个常用函数经典SQL语句大全.zip

    Oracle_Sql语句资料oracle+110个常用函数经典SQL语句大全,可供学习参考。

    Oracle SQL语句性能优化

    Oracle SQL语句性能优化是数据库管理中的关键环节,直接影响到系统的响应速度和资源利用效率。本文将详细探讨优化Oracle SQL语句的几个核心策略。 1. 选择合适的优化器 Oracle 提供了三种优化器:RULE(基于规则)...

    Oracle数据库sql语句 跟踪器

    Oracle数据库SQL语句跟踪器,通常被称为SQL Monitor,是一种强大的工具,用于监控和分析数据库中的SQL语句执行情况。在Oracle环境中,理解SQL语句的行为是优化数据库性能的关键。SQL Monitor提供实时视图,帮助DBA...

    oracle常用sql语句大全 注释完整 放心使用

    本资料集包含了Oracle数据库中常用SQL语句的大全,注释完整,非常适合初学者和有经验的开发者参考使用。 1. **数据查询(SELECT语句)**: - `SELECT * FROM table_name;`:查询表table_name中的所有列。 - `...

    Oracle数据库SQL语句大全

    Oracle数据库SQL语句大全

    Oracle基本sql语句

    Oracle数据库是全球最广泛使用的数据库管理系统之一,它遵循SQL标准,提供了一套强大的SQL语句,使得数据库操作变得高效和灵活。在Oracle数据库中,SQL语句是与数据库进行交互的主要方式。本文将总结一些Oracle基本...

    ORACLE数据库SQL语句美化器

    对ORACLE-SQL进行一些布局优化,更新它的格式

    [自己开发]一款非常好用的抓取Oracle数据库SQL语句的工具

    Oracle SQL Profiler,自己设计算法写的一款非常好用的抓取Oracle数据库SQL语句的工具,可以再没有源码的情况下监控ORACLE数据库服务器的v$sqlarea视图抓取出从点击开始按钮到点击结束按钮期间执行过的SQL语句。...

    oracle利用批处理文件执行SQL语句,bat连接oracle数据库并执行语句

    在这个场景中,我们可以利用批处理文件来简化Oracle数据库的操作,如删除用户、创建用户、创建表和触发器,以及执行其他SQL语句。 首先,我们需要了解如何在批处理文件中连接Oracle数据库。这通常通过Oracle的...

Global site tag (gtag.js) - Google Analytics