1.查詢有enqueue等待的事件
SELECT b.SID, b.serial#, b.username, machine, event, wait_time,
CHR (BITAND (p1, -16777216) / 16777215)
|| CHR (BITAND (p1, 16711680) / 65535) 「Enqueue Type」
FROM v$session_wait a, v$session b
WHERE a.event NOT LIKE ‘SQL*N%’
AND a.event NOT LIKE ‘rdbms%’
AND a.SID = b.SID
AND b.SID > 8
AND a.event = ‘enqueue’
ORDER BY username;
2
如何肯定哪一個表空間讀寫頻繁?
select name,phyrds,phywrts,readtim,writetim
from v$filestat a,v$dbfile b
where a.file# = b.file#
order by readtim desc
3
Library Cache Pin/Lock Pile Up
SELECT s.sid, kglpnmod 「Mode」, kglpnreq 「Req」, SPID 「OS Process」
FROM v$session_wait w, x$kglpn p, v$session s ,v$process o
WHERE p.kglpnuse=s.saddr
AND kglpnhdl=w.p1raw
and w.event like ‘%library cache pin%’
and s.paddr=o.addr
4全表掃描的表
SQL>col name for a30
SQL>select name,value from v$sysstat
2 where name in (’table scans(short tables)’,’table scans(long tables)’);
5
查詢SQL語句執行時,硬語法分析的次數
select name,value
from v$sysstat
where name like ‘parse count%’;
6
該項顯示buffer cache大小是否合適。
公式:1-((physical reads-physical reads direct-physical reads direct (lob)) / session logical reads)
執行:
select 1-((a.value-b.value-c.value)/d.value)
from v$sysstat a,v$sysstat b,v$sysstat c,v$sysstat d
where a.name=’physical reads’ and
b.name=’physical reads direct’ and
c.name=’physical reads direct (lob)’ and
d.name=’session logical reads’;
7
該項顯示buffer命中率。
公式:1-(physical reads/ (db block gets+consistent gets))
執行:
select 1 – (sum(decode(name, ‘physical reads’, value, 0)) /
(sum(decode(name, ‘db block gets’, value, 0)) +
sum(decode(name, ‘consistent gets’, value, 0))))
「Buffer Hit Ratio」
from v$sysstat;
8
Soft parse ratio:這項將顯示系統是否有太多硬解析。該值將會與原始統計數據對比以確保精確。例如,軟解析率僅爲0.2則表示硬解析率過高。不過,若是總解析量(parse count total)偏低,這項值能夠被忽略。
公式:1 – ( parse count (hard) / parse count (total) )
執行:
select 1-(a.value/b.value)
from v$sysstat a,v$sysstat b
Where a.name=’parse count (hard)’ and b.name=’parse count (total)’;
9
In-memory sort ratio:該項顯示內存中完成的排序所佔比例。最理想狀態下,在OLTP系統中,大部分排序不只小而且可以徹底在內存裏完成排序。
公式:sorts (memory) / ( sorts (memory) + sorts (disk) )
執行:
select a.value/(b.value+c.value)
from v$sysstat a,v$sysstat b,v$sysstat c
where a.name=’sorts (memory)’ and
b.name=’sorts (memory)’ and c.name=’sorts (disk)’;
10
Parse to execute ratio:在生產環境,最理想狀態是一條sql語句一次解析多數運行。
公式:1 – (parse count/execute count)
執行:
select 1-(a.value/b.value)
from v$sysstat a,v$sysstat b
where a.name=’parse count (total)’ and b.name=’execute count’;
11
Parse CPU to total CPU ratio:該項顯示總的CPU花費在執行及解析上的比率。若是這項比率較低,說明系統執行了太多的解析。
公式:1 – (parse time cpu / CPU used by this session)
執行:
select 1-(a.value/b.value)
from v$sysstat a,v$sysstat b
where a.name=’parse time cpu’ and
b.name=’CPU used by this session’;sql
12
Parse time CPU to parse time elapsed:一般,該項顯示鎖競爭比率。這項比率計算
是否時間花費在解析分配給CPU進行週期運算(即生產工做)。解析時間花費不在CPU週期運算一般表示因爲鎖競爭致使了時間花費
公式:parse time cpu / parse time elapsed
執行:
select a.value/b.value
from v$sysstat a,v$sysstat b
where a.name=’parse time cpu’ and b.name=’parse time elapsed’;數據庫
13
從V$SYSSTAT獲取負載間檔(Load Profile)數據
負載間檔是監控系統吞吐量和負載變化的重要部分,該部分提供以下每秒和每一個事務的統計信息:logons cumulative, parse count (total), parse count (hard), executes, physical reads, physical writes, block changes, and redo size.
被格式化的數據可檢查’rates’是否太高,或用於對比其它基線數據設置爲識別system profile在期間如何變化。例如,計算每一個事務中block changes可用以下公式:
db block changes / ( user commits + user rollbacks )
執行:
select a.value/(b.value+c.value)
from v$sysstat a,v$sysstat b,v$sysstat c
where a.name=’db block changes’ and
b.name=’user commits’ and c.name=’user rollbacks’;
其它計算統計以衡量負載方式,以下:
l Blocks changed for each read:這項顯示出block changes在block reads中的比例。它將指出是否系統主要用於只讀訪問或是主要進行諸多數據操做(如:inserts/updates/deletes)
公式:db block changes / session logical reads
執行:
select a.value/b.value
from v$sysstat a,v$sysstat b
where a.name=’db block changes’ and
b.name=’session logical reads’ ;c#
14
Rows for each sort:
公式:sorts (rows) / ( sorts (memory) + sorts (disk) )
執行:
select a.value/(b.value+c.value)
from v$sysstat a,v$sysstat b,v$sysstat c
where a.name=’sorts (rows)’ and
b.name=’sorts (memory)’ and c.name=’sorts (disk)’;緩存
15
查看某表的約束條件
SQL>select constraint_name, constraint_type,search_condition, r_constraint_name
from user_constraints where table_name = upper(’&table_name’);
SQL>select c.constraint_name,c.constraint_type,cc.column_name
from user_constraints c,user_cons_columns cc
where c.owner = upper(’&table_owner’) and c.table_name = upper(’&table_name’)
and c.owner = cc.owner and c.constraint_name = cc.constraint_name
order by cc.position;
16
查看錶空間的名稱及大小
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;session
17
查看回滾段名稱及大小
select segment_name, tablespace_name, r.status,
(initial_extent/1024) InitialExtent,(next_extent/1024) NextExtent,
max_extents, v.curext CurExtent
From dba_rollback_segs r, v$rollstat v
Where r.segment_id = v.usn(+)
order by segment_name ;ui
18
查看錶空間的使用狀況
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;
19
當移動一個表的多個分區時的腳本
BEGIN
FOR x IN (SELECT partition_name
FROM user_tab_partitions
WHERE table_name = ‘BIG_TABLE2′)
LOOP
EXECUTE IMMEDIATE ‘alter table big_table2 move partition ‘
|| x.partition_name;
END LOOP;
END;
/
20
查看LOCKthis
SELECT /*+ ORDERED USE_HASH(H,R) */
H.SID HOLD_SID,
R.SID WAIT_SID,
decode(H.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 Invalidation」,
」LS」, 」Log Start or Switch」,
」RW」, 」Row Wait」,
」SQ」, 」Sequence Number」,
」TE」, 」Extend Table」,
」TT」, 」Temp Table」,
H.type) type,
decode(H.lmode,
0, 」None」, 1, 」Null」,
2, 」Row-S (SS)」, 3, 」Row-X (SX)」,
4, 」Share」, 5, 」S/Row-X (SSX)」,
6, 」Exclusive」, to_char(H.lmode)) hold,
decode(r.request, 0, 」None」,
1, 」Null」, 2, 」Row-S (SS)」,
3, 」Row-X (SX)」, 4, 」Share」,
5, 」S/Row-X (SSX)」,6, 」Exclusive」,
to_char(R.request)) request,
R.ID1,R.ID2,R.CTIME
FROM VLOCK H,VLOCK R
WHERE H.BLOCK = 1 AND R.BLOCK=0
and H.TYPE <> 」MR」 AND R.TYPE <> 」MR」
AND H.ID1 = R.ID1 AND H.ID2 = R.ID2
21
查看ORACLE運行的OS平臺
SQL> run
1 begin
2 dbms_output.put_line(
3 dbms_utility.port_string);
4* end;
Linuxi386/Linux-2.0.34-8.1.0
PL/SQL 過程已成功完成。spa
22
查看空間詳細使用狀況
CREATE OR REPLACE PROCEDURE show_space (
p_segname IN VARCHAR2,
p_owner IN VARCHAR2 DEFAULT USER,
p_type IN VARCHAR2 DEFAULT ‘TABLE’,
p_partition IN VARCHAR2 DEFAULT NULL
)
AS
l_total_blocks NUMBER;
l_total_bytes NUMBER;
l_unused_blocks NUMBER;
l_unused_bytes NUMBER;
l_lastusedextfileid NUMBER;
l_lastusedextblockid NUMBER;
l_last_used_block NUMBER;
PROCEDURE p (p_label IN VARCHAR2, p_num IN NUMBER)
IS
BEGIN
DBMS_OUTPUT.put_line (RPAD (p_label, 40, ‘.’) || p_num);
END;
BEGIN
DBMS_SPACE.unused_space
(segment_owner => p_owner,
segment_name => p_segname,
segment_type => p_type,
partition_name => p_partition,
total_blocks => l_total_blocks,
total_bytes => l_total_bytes,
unused_blocks => l_unused_blocks,
unused_bytes => l_unused_bytes,
last_used_extent_file_id => l_lastusedextfileid,
last_used_extent_block_id => l_lastusedextblocki
last_used_block => l_last_used_block
);
p (‘Total Blocks’, l_total_blocks);
p (‘Total Bytes’, l_total_bytes);
p (‘Unused Blocks’, l_unused_blocks);
p (‘Unused Bytes’, l_unused_bytes);
p (‘Last Used Ext FileId’, l_lastusedextfileid);
p (‘Last Used Ext BlockId’, l_lastusedextblockid);
p (‘Last Used Block’, l_last_used_block);
END;
/日誌
24
顯示緩衝區的相關SQL
SELECT tch, file#, dbablk,
CASE
WHEN obj = 4294967295
THEN ‘rbs/compat segment’
ELSE (SELECT MAX ( ‘(‘
|| object_type
|| ‘) ‘
|| owner
|| ‘.’
|| object_name
)
|| DECODE (COUNT (*), 1, 」, ‘ maybe!’)
FROM dba_objects
WHERE data_object_id = x.obj)
END what
FROM (SELECT tch, file#, dbablk, obj
FROM x$bh
WHERE state <> 0
ORDER BY tch DESC) x
WHERE ROWNUM <= 5;
25
獲取生成的根據文件名
select c.value ||’/’ || d.instance_name || ‘_ora_’ ||a.spid || ‘.trc’ trace
from v$process a,v$session b,v$parameter c,v$instance d
where a.addr=b.paddr
and b.audsid=userenv(‘sessionid’)
and c.name=’user_dump_dest’
/
在v$session_longops視圖中,sofar字段表示已經掃描的塊數,totalwork表示總得須要掃描的塊數,因此咱們便可以對正在運行的長查詢進行監控,好比在索引建立時,查看索引建立的進度,也能夠查看系統中以往的長查詢。。。
col opname format a32
col target_desc format a32
col perwork format a12
set lines 131
select sid,OPNAME,TARGET_DESC,sofar,TOTALWORK,trunc(sofar/totalwork*100,2)||’%’ as perwork
from v$session_longops where sofar!=totalwork;
set lines 121
set pages 999
col opname format a29
col target format a29
col target_desc format a12
col perwork format a12
col remain format 99
col start_time format a21
col sofar format 99999999
col totalwork format 99999999
col sql_text format a101
col bufgets format 99999999
select opname,target,to_char(start_time,’yy-mm-dd:hh24:mi:ss’) start_time,elapsed_seconds elapsed,
executions execs,buffer_gets/decode(executions,0,1,executions) bufgets,module,sql_text
from v$session_longops sl,v$sqlarea sa
where sl.sql_hash_value = sa.hash_value and upper(substr(module,1,4)) <> ‘RMAN’ and substr(opname,1,4) <> ‘RMAN’
and module <> ‘SQL*Plus’ and sl.start_time>trunc(sysdate)
order by start_time;code
1. 監控事例的等待
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. 回滾段的爭用狀況
select name, waits, gets, waits/gets 「Ratio」 from v$rollstat a, v$rollname b where a.usn = b.usn;
3. 監控表空間的 I/O 比例
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 比例
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.在某個用戶下找全部的索引
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 的命中率
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 中字典緩衝區的命中率
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%
select sum(pins) 「Total Pins」, sum(reloads) 「Total Reloads」, sum(reloads)/sum(pins) *100 libcache from v$librarycache; select sum(pinhits-reloads)/sum(pins) 「hit radio」,sum(reloads)/sum(pins) 「reload percent」 from v$librarycache;9. 顯示全部數據庫對象的類別和大小 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% 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
SELECT name, value FROM v$sysstat WHERE name IN (‘sorts (memory)’, ‘sorts (disk)’);
12. 監控當前數據庫誰在運行什麼SQL語句
SELECT osuser, username, sql_text from v$session a, v$sqltext b where a.sql_address =b.address order by address, piece;
13. 監控字典緩衝區
SELECT (SUM(PINS – RELOADS)) / SUM(PINS) 「LIB CACHE」 FROM V$LIBRARYCACHE; SELECT (SUM(GETS – GETMISSES – USAGE – FIXED)) / SUM(GETS) 「ROW CACHE」 FROM V$ROWCACHE; SELECT SUM(PINS) 「EXECUTIONS」, SUM(RELOADS) 「CACHE MISSES WHILE EXECUTING」 FROM
V$LIBRARYCACHE; 後者除之前者,此比率小於1%,接近0%爲好。 SELECT SUM(GETS) 「DICTIONARY GETS」,SUM(GETMISSES) 「DICTIONARY CACHE GET MISSES」 FROM V$ROWCACHE
14. 找ORACLE字符集
select * from sys.props$ where name=’NLS_CHARACTERSET’;
15. 監控 MTS
select busy/(busy+idle) 「shared servers busy」 from v$dispatcher; 此值大於0.5時,參數需加大 select sum(wait)/sum(totalq) 「dispatcher waits」 from v$queue where type=’dispatcher’; select count(*) from v$dispatcher; select servers_highwater from v$mts; servers_highwater接近mts_max_servers時,參數需加大
16. 碎片程度
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; 查看碎片程度高的表 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. 表、索引的存儲狀況檢查
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; select segment_name,count(*) from dba_extents where segment_type=’INDEX’ and owner=’&owner’ group by segment_name;1八、找使用CPU多的用戶session 12是cpu used by this session 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;
尋找CPU使用過量的session ,找出高CPU利用率的SQL:
SELECT /*+ ORDERED */sql_textFROM v$sqltext aWHERE (a.hash_value, a.address) IN(SELECT decode(sql_hash_value, 0, prev_hash_value, sql_hash_value), decode(sql_hash_value, 0, prev_sql_addr, sql_address)FROM v$session bWHERE b.paddr = (SELECT addrFROM v$process cWHERE c.spid = ‘&pid’))ORDER BY piece ASC;