1、 求当前会话的SID,SERIAL#
SELECT Sid, Serial#
FROM V$session
WHERE Audsid = Sys_Context('USERENV', 'SESSIONID');
2、 查询session的OS进程ID
SELECT p.Spid "OS Thread", b.NAME "Name-User", s.Program, s.Sid, s.Serial#,
s.Osuser, s.Machine
FROM V$process p, V$session s, V$bgprocess b
WHERE p.Addr = s.Paddr
AND p.Addr = b.Paddr
And (s.sid=&1 or p.spid=&1)
UNION ALL
SELECT p.Spid "OS Thread", s.Username "Name-User", s.Program, s.Sid,
s.Serial#, s.Osuser, s.Machine
FROM V$process p, V$session s
WHERE p.Addr = s.Paddr
And (s.sid=&1 or p.spid=&1)
AND s.Username IS NOT NULL;
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 = &sid );
4、查找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 = '&obj'
ORDER BY s.Username, s.Osuser
5、查看有哪些用户连接
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
6、根据v.sid查看对应连接的资源占用等情况
SELECT n.NAME, v.VALUE, n.CLASS, n.Statistic#
FROM V$statname n, V$sesstat v
WHERE v.Sid = &sid
AND v.Statistic# = n.Statistic#
ORDER BY n.CLASS, n.Statistic#
7、查询耗资源的进程(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
8、查看锁(lock)情况
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
9、查看等待(wait)情况
SELECT Ws.CLASS, Ws.COUNT COUNT, SUM(Ss.VALUE) Sum_Value
FROM V$waitstat Ws, V$sysstat Ss
WHERE Ss.NAME IN ('db block gets', 'consistent gets')
GROUP BY Ws.CLASS, Ws.COUNT
10、求process/session的状态
SELECT p.Pid, p.Spid, s.Program, s.Sid, s.Serial#
FROM V$process p, V$session s
WHERE s.Paddr = p.Addr;
11、求谁阻塞了某个session(10g)
SELECT Sid, Username, Event, Blocking_Session, Seconds_In_Wait, Wait_Time
FROM V$session
WHERE State IN ('WAITING')
AND Wait_Class != 'Idle';
12、查会话的阻塞
col user_name format a32
SELECT /*+ rule */
Lpad(' ', Decode(l.Xidusn, 0, 3, 0)) || l.Oracle_Username User_Name,
o.Owner, o.Object_Name, s.Sid, s.Serial#
FROM V$locked_Object l, Dba_Objects o, V$session s
WHERE l.Object_Id = o.Object_Id
AND l.Session_Id = s.Sid
ORDER BY o.Object_Id, Xidusn DESC;
col username format a15
col lock_level format a8
col owner format a18
col object_name format a32
SELECT /*+ rule */
s.Username,
Decode(l.TYPE, 'tm', 'table lock', 'tx', 'row lock', NULL) Lock_Level,
o.Owner, o.Object_Name, s.Sid, s.Serial#
FROM V$session s, V$lock l, Dba_Objects o
WHERE l.Sid = s.Sid
AND l.Id1 = o.Object_Id(+)
AND s.Username IS NOT NULL;
13、求等待的事件及会话信息/求会话的等待及会话信息
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%'
ORDER BY s.Username;
SELECT s.Sid, s.Username, Sw.Event, Sw.Wait_Time, Sw.State,
Sw.Seconds_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 s.Username;
14、求会话等待的file_id/block_id
col event format a24
col p1text format a12
col p2text format a12
col p3text format a12
SELECT Sid, Event, P1text, P1, P2text, P2, P3text, P3
FROM V$session_Wait
WHERE Event NOT LIKE '%SQL%'
AND Event NOT LIKE '%rdbms%'
AND Event NOT LIKE '%mon%'
ORDER BY Event;
SELECT NAME, Wait_Time
FROM V$latch l
WHERE EXISTS (SELECT 1
FROM (SELECT Sid, Event, P1text, P1, P2text, P2, P3text, P3
FROM V$session_Wait
WHERE Event NOT LIKE '%SQL%'
AND Event NOT LIKE '%rdbms%'
AND Event NOT LIKE '%mon%') x
WHERE x.P1 = l.Latch#);
15、求会话等待的对象
col owner format a18
col segment_name format a32
col segment_type format a32
SELECT Owner, Segment_Name, Segment_Type
FROM Dba_Extents
WHERE File_Id = &File_Id
AND &Block_Id BETWEEN Block_Id AND Block_Id + Blocks - 1;
16、求出某个进程,并对它进行跟踪
SELECT s.Sid, s.Serial#
FROM V$session s, V$process p
WHERE s.Paddr = p.Addr
AND p.Spid = &1;
Exec Dbms_System.Set_Sql_Trace_In_Session(&1, &2, TRUE);
Exec Dbms_System.Set_Sql_Trace_In_Session(&1, &2, FALSE);
17、求当前session的跟踪文件
SELECT P1.VALUE || '/' || P2.VALUE || '_ora_' || p.Spid || '.ora' Filename
FROM V$process p, V$session s, V$parameter P1, V$parameter P2
WHERE P1.NAME = 'user_dump_dest'
AND P2.NAME = 'instance_name'
AND p.Addr = s.Paddr
AND s.Audsid = Userenv('SESSIONID')
AND p.Background IS NULL
AND Instr(p.Program, 'CJQ') = 0;
18、求出锁定的对象
SELECT Do.Object_Name, Session_Id, Process, Locked_Mode
FROM V$locked_Object Lo, Dba_Objects Do
WHERE Lo.Object_Id = Do.Object_Id;
空间状态相关内容
1、查看表空间的名称及大小
SELECT t.Tablespace_Name, Round(SUM(Bytes / (1024 * 1024)), 0) Ts_Size
FROM Dba_Tablespaces t, Dba_Data_Files d
WHERE t.Tablespace_Name = d.Tablespace_Name
GROUP BY t.Tablespace_Name;
2、查看表空间物理文件的名称及大小
SELECT Tablespace_Name, File_Id, File_Name,
Round(Bytes / (1024 * 1024), 0) Total_Space
FROM Dba_Data_Files
ORDER BY Tablespace_Name;
3、求表空间的未用空间
col mbytes format 9999.9999
SELECT Tablespace_Name, SUM(Bytes) / 1024 / 1024 Mbytes
FROM Dba_Free_Space
GROUP BY Tablespace_Name;
4、察看数据库的大小,和空间使用情况
SELECT SUM(Bytes) / (1024 * 1024) AS Free_Space, Tablespace_Name
FROM Dba_Free_Space
GROUP BY Tablespace_Name;
SELECT a.Tablespace_Name, a.Bytes Total, b.Bytes Used, c.Bytes Free,
(b.Bytes * 100) / a.Bytes "% USED",
(c.Bytes * 100) / a.Bytes "% FREE"
FROM Sys.Sm$ts_Avail a, Sys.Sm$ts_Used b, Sys.Sm$ts_Free c
WHERE a.Tablespace_Name = b.Tablespace_Name
AND a.Tablespace_Name = c.Tablespace_Name;
5、查看表空间的文件使用情况
SELECT b.File_Id 文件id, b.Tablespace_Name 表空间, b.File_Name 物理文件名,
b.Bytes 总字节数, (b.Bytes - SUM(Nvl(a.Bytes, 0))) 已使用,
SUM(Nvl(a.Bytes, 0)) 剩余,
SUM(Nvl(a.Bytes, 0)) / (b.Bytes) * 100 剩余百分比
FROM Dba_Free_Space a,
Dba_Data_Files b where a.File_Id = b.File_Id group BY b.Tablespace_Name,
b.File_Name, b.File_Id, b.Bytes order BY b.Tablespace_Name
--/ dba_Free_Space --表空间剩余空间状况
-- dba_Data_Files --数据文件空间占用情况
6、 查询表空间的碎片程度
SELECT Tablespace_Name, COUNT(Tablespace_Name)
FROM Dba_Free_Space
GROUP BY Tablespace_Name
HAVING COUNT(Tablespace_Name) > 10;
alter tablespace name coalesce;
alter table name deallocate unused;
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;
SELECT Tablespace_Name, SUM(Bytes), MAX(Bytes), COUNT(Block_Id)
FROM Dba_Free_Space
GROUP BY Tablespace_Name;
性能相关内容
1、捕捉运行很久的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;
2、求DISK READ较多的SQL
SELECT St.Sql_Text
FROM V$sql s, V$sqltext St
WHERE s.Address = St.Address
AND s.Hash_Value = St.Hash_Value
AND s.Disk_Reads > 300;
3、求DISK SORT严重的SQL
SELECT Sess.Username, SQL.Sql_Text, Sort1.Blocks
FROM V$session Sess, V$sqlarea SQL, V$sort_Usage Sort1
WHERE Sess.Serial# = Sort1.Session_Num
AND Sort1.Sqladdr = SQL.Address
AND Sort1.Sqlhash = SQL.Hash_Value
AND Sort1.Blocks > 200;
4、监控索引是否使用
alter index &index_name monitoring usage;
alter index &index_name nomonitoring usage;
select * from v$object_usage where index_name = &index_name;
5、求数据文件的I/O分布
SELECT Df.NAME, Phyrds, Phywrts, Phyblkrd, Phyblkwrt, Singleblkrds, Readtim,
Writetim
FROM V$filestat Fs, V$dbfile Df
WHERE Fs.File# = Df.File#
ORDER BY Df.NAME;
6、查看还没提交的事务
select * from v$locked_object;
select * from v$transaction;
7、回滚段查看
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
8、查看系统请求情况
SELECT Decode(NAME, 'summed dirty write queue length', VALUE) /
Decode(NAME, 'write requests', VALUE) "Write Request Length"
FROM V$sysstat
WHERE NAME IN ('summed dirty queue length', 'write requests')
AND VALUE > 0;
9、计算data buffer 命中率
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# = 40
AND b.Statistic# = 41
AND c.Statistic# = 42;
SELECT NAME,
(1 - (Physical_Reads / (Db_Block_Gets + Consistent_Gets))) * 100 h_Ratio
FROM V$buffer_Pool_Statistics;
10、查看内存使用情况
SELECT Least(MAX(b.VALUE) / (1024 * 1024), SUM(a.Bytes) / (1024 * 1024)) Shared_Pool_Used,
MAX(b.VALUE) / (1024 * 1024) Shared_Pool_Size,
Greatest(MAX(b.VALUE) / (1024 * 1024), SUM(a.Bytes) / (1024 * 1024)) -
(SUM(a.Bytes) / (1024 * 1024)) Shared_Pool_Avail,
((SUM(a.Bytes) / (1024 * 1024)) / (MAX(b.VALUE) / (1024 * 1024))) * 100 Avail_Pool_Pct
FROM V$sgastat a, V$parameter b
WHERE (a.Pool = 'shared pool' AND a.NAME NOT IN ('free memory'))
AND b.NAME = 'shared_pool_size';
11、查看用户使用内存情况
SELECT Username, SUM(Sharable_Mem), SUM(Persistent_Mem), SUM(Runtime_Mem)
FROM Sys.v_$sqlarea a, Dba_Users b
WHERE a.Parsing_User_Id = b.User_Id
GROUP BY Username;
12、查看对象的缓存情况
SELECT Owner, Namespace, TYPE, NAME, Sharable_Mem, Loads, Executions, Locks,
Pins, Kept
FROM V$db_Object_Cache
WHERE TYPE NOT IN
('NOT LOADED', 'NON-EXISTENT', 'VIEW', 'TABLE', 'SEQUENCE')
AND Executions > 0
AND Loads > 1
AND Kept = 'NO'
ORDER BY Owner, Namespace, TYPE, Executions DESC;
SELECT TYPE, COUNT(*)
FROM V$db_Object_Cache
GROUP BY TYPE;
13、查看库缓存命中率
SELECT Namespace, Gets, Gethitratio * 100 Gethitratio, Pins,
Pinhitratio * 100 Pinhitratio, Reloads, Invalidations
FROM V$librarycache
14、查看某些用户的hash
SELECT a.Username, COUNT(b.Hash_Value) Total_Hash,
COUNT(b.Hash_Value) - COUNT(UNIQUE(b.Hash_Value)) Same_Hash,
(COUNT(UNIQUE(b.Hash_Value)) / COUNT(b.Hash_Value)) * 100 u_Hash_Ratio
FROM Dba_Users a, V$sqlarea b
WHERE a.User_Id = b.Parsing_User_Id
GROUP BY a.Username;
15、查看字典命中率
SELECT (SUM(Getmisses) / SUM(Gets)) Ratio
FROM V$rowcache;
16、查看undo段的使用情况
SELECT d.Segment_Name, Extents, Optsize, Shrinks, Aveshrink, Aveactive,
d.Status
FROM V$rollname n, V$rollstat s, Dba_Rollback_Segs d
WHERE d.Segment_Id = n.Usn(+)
AND d.Segment_Id = s.Usn(+);
17、求归档日志的切换频率(生产系统可能时间会很长)
SELECT Start_Recid, Start_Time, End_Recid, End_Time, Minutes
FROM (SELECT Test.*, Rownum AS Rn
FROM (SELECT b.Recid Start_Recid,
To_Char(b.First_Time, 'yyyy-mm-dd hh24:mi:ss') Start_Time,
a.Recid End_Recid,
To_Char(a.First_Time, 'yyyy-mm-dd hh24:mi:ss') End_Time,
Round(((a.First_Time - b.First_Time) * 24) * 60, 2) Minutes
FROM V$log_History a, V$log_History b
WHERE a.Recid = b.Recid + 1
AND b.First_Time > SYSDATE - 1
ORDER BY a.First_Time DESC) Test) y
WHERE y.Rn < 30
18、求回滚段正在处理的事务
SELECT a.NAME, b.Xacts, c.Sid, c.Serial#, d.Sql_Text
FROM V$rollname a, V$rollstat b, V$session c, V$sqltext d, V$transaction e
WHERE a.Usn = b.Usn
AND b.Usn = e.Xidusn
AND c.Taddr = e.Addr
AND c.Sql_Address = d.Address
AND c.Sql_Hash_Value = d.Hash_Value
ORDER BY a.NAME, c.Sid, d.Piece;
19、求某个事务的重做信息(bytes)
SELECT s.NAME, m.VALUE
FROM V$mystat m, V$statname s
WHERE m.Statistic# = s.Statistic#
AND s.NAME LIKE '%redo size%';
20、求cache中缓存超过其5%的对象
SELECT o.Owner, o.Object_Type, o.Object_Name, COUNT(b.Objd)
FROM V$bh b, Dba_Objects o
WHERE b.Objd = o.Object_Id
GROUP BY o.Owner, o.Object_Type, o.Object_Name
HAVING COUNT(b.Objd) > (SELECT To_Number(VALUE) * 0.05
FROM V$parameter
WHERE NAME = 'db_block_buffers');
21、求buffer cache中的块信息
SELECT o.Object_Type, Substr(o.Object_Name, 1, 10) Objname, b.Objd, b.Status,
COUNT(b.Objd)
FROM V$bh b, Dba_Objects o
WHERE b.Objd = o.Data_Object_Id
AND o.Owner = '&owner'
GROUP BY o.Object_Type, o.Object_Name, b.Objd, b.Status;
22、求日志文件的空间使用
SELECT Le.Leseq Current_Log_Sequence#,
100 * Cp.Cpodr_Bno / Le.Lesiz Percentage_Full
FROM X$kcccp Cp, X$kccle Le
WHERE Le.Leseq = Cp.Cpodr_Seq;
23、求等待中的对象
SELECT /*+rule */
s.Sid, s.Username, w.Event, o.Owner, o.Segment_Name, o.Segment_Type,
o.Partition_Name, w.Seconds_In_Wait Seconds, w.State
FROM V$session_Wait w, V$session s, Dba_Extents o
WHERE w.Event IN (SELECT NAME
FROM V$event_Name
WHERE Parameter1 = 'file#'
AND Parameter2 = 'block#'
AND NAME NOT LIKE 'control%')
AND o.Owner <> 'sys'
AND w.Sid = s.Sid
AND w.P1 = o.File_Id
AND w.P2 >= o.Block_Id
AND w.P2 < o.Block_Id + o.Blocks
24、求当前事务的重做尺寸
SELECT V$statname.NAME,VALUE
FROM V$mystat, V$statname
WHERE V$mystat.Statistic# = V$statname.Statistic#
AND V$statname.NAME = 'redo size';
25、唤醒smon去清除临时段
column pid new_value Smon
set termout off
SELECT p.Pid
FROM Sys.v_$bgprocess b, Sys.v_$process p
WHERE b.NAME = 'SMON'
AND p.Addr = b.Paddr;
/
SET Termout ON Oradebug Wakeup &Smon Undefine Smon
26、求回退率
SELECT b.VALUE / (a.VALUE + b.VALUE), a.VALUE, b.VALUE
FROM V$sysstat a, V$sysstat b
WHERE a.Statistic# = 4
AND b.Statistic# = 5;
27、求free memory
SELECT *
FROM V$sgastat
WHERE NAME = 'free memory';
SELECT a.NAME, SUM(b.VALUE)
FROM V$statname a, V$sesstat b
WHERE a.Statistic# = b.Statistic#
GROUP BY a.NAME;
查看一下谁在使用那个可以得回滚段,或者查看一下某个可以得用户在使用回滚段,
找出领回滚段不断增长的事务,再看看如何处理它,是否可以将它commit,再不行
就看看能否kill它,等等, 查看当前正在使用的回滚段的用户信息和回滚段信息:
set linesize 121
SELECT r.NAME "ROLLBACK SEGMENT NAME ", l.Sid "ORACLE PID",
p.Spid "SYSTEM PID ", s.Username "ORACLE USERNAME"
FROM V$lock l, V$process p, V$rollname r, V$session s
WHERE l.Sid = p.Pid(+)
AND s.Sid = l.Sid
AND Trunc(l.Id1(+) / 65536) = r.Usn
AND l.TYPE(+) = 'TX'
AND l.Lmode(+) = 6
ORDER BY r.NAME;
28、查看用户的回滚段的信息
SELECT s.Username, Rn.NAME
FROM V$session s, V$transaction t, V$rollstat r, V$rollname Rn
WHERE s.Saddr = t.Ses_Addr
AND t.Xidusn = r.Usn
AND r.Usn = Rn.Usn
29、查看内存中存的使用
SELECT Decode(Greatest(CLASS, 10),
10,
Decode(CLASS, 1, 'Data', 2, 'Sort', 4, 'Header', To_Char(CLASS)), 'Rollback') "Class",
SUM(Decode(Bitand(Flag, 1), 1, 0, 1)) "Not Dirty",
SUM(Decode(Bitand(Flag, 1), 1, 1, 0)) "Dirty",
SUM(Dirty_Queue) "On Dirty", COUNT(*) "Total"
FROM X$bh
GROUP BY Decode(Greatest(CLASS, 10),
10,
Decode(CLASS, 1, 'Data', 2, 'Sort', 4, 'Header', To_Char(CLASS)), 'Rollback');