DBA常用脚本 ------二、性能监控

二、性能监控 

1、数据缓冲区的命中率已经不是性能调整中的主要问题了,但是,过低的命中率肯定是不可以的,在任何情况下,我们必须保证有一个大的data buffer和一个高的命中率。 
这个语句可以获得整体的数据缓冲命中率,越高越好

 

SELECT a.VALUE + b.VALUE logical_reads, 
c.VALUE phys_reads, 
round(100*(1-c.value/(a.value+b.value)),4) hit_ratio 
FROM v$sysstat a,v$sysstat b,v$sysstat c 
WHERE a.NAME='db block gets' 
AND b.NAME='consistent gets' 
AND c.NAME='physical reads'

 


2、库缓冲说明了SQL语句的重载率,当然,一个SQL语句应当被执行的越多越好,如果重载率比较高,就考虑增加共享池大小或者是提高Bind变量的使用 
以下语句查询了Sql语句的重载率,越低越好



SELECT SUM(pins) total_pins,SUM(reloads) total_reloads, 
SUM(reloads)/SUM(pins)*100 libcache_reload_ratio 
FROM v$librarycache

 


3、用户锁,数据库的锁有的时候是比较耗费资源的,特别是发生锁等待的时候,我们必须找到发生等待的锁,有可能的话,杀掉该进程。 
这个语句将查找到数据库中所有的DML语句产生的锁,还可以发现,任何DML语句其实产生了两个锁,一个是表锁,一个是行锁。 
可以通过alter system kill session ‘sid,serial#’来杀掉会话

 

SELECT /*+ rule */ s.username, 
decode(l.type,'TM','TABLE LOCK', 
'TX','ROW LOCK', 
NULL) LOCK_LEVEL, 
o.owner,o.object_name,o.object_type, 
s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser 
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

 


4、锁与等待,如果发生了锁等待,我们可能更想知道是谁锁了表而引起谁的等待 
以下的语句可以查询到谁锁了表,而谁在等待。

 

SELECT /*+ rule */ lpad(' ',decode(l.xidusn ,0,3,0))||l.oracle_username User_name, 
o.owner,o.object_name,o.object_type,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

 


以上查询结果是一个树状结构,如果有子节点,则表示有等待发生。如果想知道锁用了哪个回滚段,还可以关联到V$rollname,其中xidusn就是回滚段的USN 

5、如果发生了事务或锁,想知道哪些回滚段正在被使用吗?其实通过事务表,我们可以详细的查询到事务与回滚段之间的关系。同时,如果关联会话表,我们则可以知道是哪个会话发动了这个事务。

 

SELECT s.USERNAME,s.SID,s.SERIAL#,t.UBAFIL "UBA filenum", 
t.UBABLK "UBA Block number",t.USED_UBLK "Number os undo Blocks Used", 
t.START_TIME,t.STATUS,t.START_SCNB,t.XIDUSN RollID,r.NAME RollName 
FROM v$session s,v$transaction t,v$rollname r 
WHERE s.SADDR=t.SES_ADDR 
AND t.XIDUSN=r.usn

 

 

 


7、如果利用会话跟踪或者是想查看某个会话的跟踪文件,那么查询到OS上的进程或线程号是非常重要的,因为文件的令名中,就包含这个信息,以下的语句可以查询到进程或线程号,由此就可以找到对应的文件。

 

SELECT p1.value||''||p2.value||'_ora_'||p.spid filename 
FROM 
v$process p, 
v$session s, 
v$parameter p1, 
v$parameter p2 
WHERE p1.name = 'user_dump_dest' 
AND p2.name = 'db_name' 
AND p.addr = s.paddr 
AND s.audsid = USERENV ('SESSIONID');

 


8、在ORACLE 9i中,可以监控索引的使用,如果没有使用到的索引,完全可以删除掉,减少DML操作时的操作。 
以下就是开始索引监控与停止索引监控的脚本

 

set heading off 
set echo off 
set feedback off 
set pages 10000 
spool start_index_monitor.sql 

SELECT 'alter index '||owner||'.'||index_name||' monitoring usage;' 
FROM dba_indexes 
WHERE owner = USER; 

spool off 
set heading on 
set echo on 
set feedback on 
------------------------------------------------ 
set heading off 
set echo off 
set feedback off 
set pages 10000 
spool stop_index_monitor.sql 

SELECT 'alter index '||owner||'.'||index_name||' nomonitoring usage;' 
FROM dba_indexes 
WHERE owner = USER; 

spool off 
set heading on 
set echo on 
set feedback on

 

如果需要监控更多的用户,可以将owner=User改写成别的 
监控结果在视图v$object_usage中查询
感谢fenng,他提供了一个更新版的show_space脚本



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 ) 
-- This procedure uses AUTHID CURRENT USER so it can query DBA_* 
-- views using privileges from a ROLE and so it can be installed 
-- once per database, instead of once per user who wanted to use it. 
AUTHID CURRENT_USER 
as 
l_free_blks number; 
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; 
l_segment_space_mgmt varchar2(255); 
l_unformatted_blocks number; 
l_unformatted_bytes number; 
l_fs1_blocks number; l_fs1_bytes number; 
l_fs2_blocks number; l_fs2_bytes number; 
l_fs3_blocks number; l_fs3_bytes number; 
l_fs4_blocks number; l_fs4_bytes number; 
l_full_blocks number; l_full_bytes number; 

-- Inline procedure to print out numbers nicely formatted 
-- with a simple label. 
PROCEDURE p( p_label in varchar2, p_num in number ) 
IS 
BEGIN 
dbms_output.put_line( rpad(p_label,40,'.') || 
to_char(p_num,'999,999,999,999') ); 
END; 
BEGIN 
-- This query is executed dynamically in order to allow this procedure 
-- to be created by a user who has access to DBA_SEGMENTS/TABLESPACES 
-- via a role as is customary. 
-- NOTE: at runtime, the invoker MUST have access to these two 
-- views! 
-- This query determines if the object is an ASSM object or not. 
BEGIN 
EXECUTE IMMEDIATE 
'select ts.segment_space_management 
FROM dba_segments seg, dba_tablespaces ts 
WHERE seg.segment_name = :p_segname 
AND (:p_partition is null or 
seg.partition_name = :p_partition) 
AND seg.owner = :p_owner 
AND seg.tablespace_name = ts.tablespace_name' 
INTO l_segment_space_mgmt 
USING p_segname, p_partition, p_partition, p_owner; 
EXCEPTION 
WHEN too_many_rows THEN 
dbms_output.put_line 
( 'This must be a partitioned table, use p_partition => '); 
RETURN; 
END; 

-- If the object is in an ASSM tablespace, we must use this API 
-- call to get space information; else we use the FREE_BLOCKS 
-- API for the user managed segments. 
IF l_segment_space_mgmt = 'AUTO' 
THEN 
dbms_space.space_usage 
( p_owner, p_segname, p_type, l_unformatted_blocks, 
l_unformatted_bytes, l_fs1_blocks, l_fs1_bytes, 
l_fs2_blocks, l_fs2_bytes, l_fs3_blocks, l_fs3_bytes, 
l_fs4_blocks, l_fs4_bytes, l_full_blocks, l_full_bytes, p_partition); 

p( 'Unformatted Blocks ', l_unformatted_blocks ); 
p( 'FS1 Blocks (0-25) ', l_fs1_blocks ); 
p( 'FS2 Blocks (25-50) ', l_fs2_blocks ); 
p( 'FS3 Blocks (50-75) ', l_fs3_blocks ); 
p( 'FS4 Blocks (75-100)', l_fs4_blocks ); 
p( 'Full Blocks ', l_full_blocks ); 
ELSE 
dbms_space.free_blocks( 
segment_owner => p_owner, 
segment_name => p_segname, 
segment_type => p_type, 
freelist_group_id => 0, 
free_blks => l_free_blks); 

p( 'Free Blocks', l_free_blks ); 
END IF; 

-- And then the unused space API call to get the rest of the 
-- information. 
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_LastUsedExtBlockId, 
LAST_USED_BLOCK => l_LAST_USED_BLOCK ); 

p( 'Total Blocks', l_total_blocks ); 
p( 'Total Bytes', l_total_bytes ); 
p( 'Total MBytes', trunc(l_total_bytes/1024/1024) ); 
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;

 


隐含参数: 
select a.ksppinm "parameter ", a.ksppdesc "descriptoin " 
from x$ksppi a,x$ksppcv b,x$ksppsv c 
where a.indx=b.indx and a.indx=c.indx and a.ksppinm like '/_%' escape '/';

Check OS process id from Oracle sid

 


select spid from v$process 
where addr in ( select paddr from v$session where sid=[$sid) ]

 


Check Oracle sid from OS process id

 

select sid from v$session 
where paddr in ( select addr from v$process where spid=[$pid) ]

 


Check current SQL in a session

 

select SQL_TEXT from V$SQLTEXT 
where HASH_VALUE = 
( select SQL_HASH_VALUE from v$session 
where sid = &sid) 
order by PIECE

 


Checking v$session_wait

 

select * from v$session_wait 
where event not like 'rdbms%' 
and event not like 'SQL*N%' 
and event not like '%timer';

 


Dictionary Cache Hits

 

SELECT sum(getmisses)/sum(gets) FROM v$rowcache; 
/*It should be < 15%, otherwise Add share_pool_size*/

 


Check DB object name from file id and block#

 

select owner,segment_name,segment_type 
from dba_extents 
where file_id = [$fno and &dno between block_id and block_id + blocks – 1 ]


#寻找hot block 
select /*+ ordered */ 
e.owner ||'.'|| e.segment_name segment_name, 
e.extent_id extent#, 
x.dbablk - e.block_id + 1 block#, 
x.tch, 
l.child# 
from 
sys.v$latch_children l, 
sys.x$bh x, 
sys.dba_extents e 
where 
l.name = 'cache buffers chains' and 
l.sleeps > &sleep_count and 
x.hladdr = l.addr and 
e.file_id = x.file# and 
x.dbablk between e.block_id and e.block_id + e.blocks - 1; 

#找出每个文件上的等待事件 
select df.name,kf.count from v$datafile df,x$kcbfwait kf where (kf.indx+1)=df.file#; 

#找出引起等待事件的SQL语句. 
select sql_text from v$sqlarea a,v$session b,v$session_wait c where a.address=b.sql_address and b.sid=c.sid and c.event=[$ll] 

#监控共享池中哪个对象引起了大的内存分配 
SELECT * FROM X$KSMLRU WHERE ksmlrsiz > 0;

判断你是从pfile启动还是spfile启动的简单方法!!!

判断你是从pfile启动还是spfile启动的简单方法!!! 

select decode(count(*), 1, 'spfile', 'pfile' ) 
from v$spparameter 
where rownum=1 
and isspecified='TRUE' 


DECODE 
------ 
spfile 

--分析数据库性能的SQL

--用于查看哪些实例的哪些操作使用了大量的临时段

SELECT to_number(decode(SID, 65535, NULL, SID)) sid, 
operation_type OPERATION,trunc(EXPECTED_SIZE/1024) ESIZE, 
trunc(ACTUAL_MEM_USED/1024) MEM, trunc(MAX_MEM_USED/1024) "MAX MEM",
NUMBER_PASSES PASS, trunc(TEMPSEG_SIZE/1024) TSIZE 
FROM V$SQL_WORKAREA_ACTIVE 
ORDER BY 1,2; 
---查询有热块查询的SQL语句
select hash_value 
from v$sqltext a,
(select distinct a.owner,a.segment_name,a.segment_type from 
dba_extents a,
(select dbarfil,dbablk 
from (select dbarfil,dbablk 
from x$bh order by tch desc) where rownum < 11) b
where a.RELATIVE_FNO = b.dbarfil
and a.BLOCK_ID <= b.dbablk and a.block_id + a.blocks > b.dbablk) b
where a.sql_text like '%'||b.segment_name||'%' and b.segment_type = 'TABLE'
order by a.hash_value,a.address,a.piece;
--全表扫描
select opname,target,b.num_rows,b.tablespace_name,count(target) from v$session_longops a,all_all_tables b
where a.TARGET=b.owner||'.'||b.table_name
having count(target)>10 group by opname,target,b.num_rows,b.tablespace_name
--查看磁盘排序和缓存排序次数
select to_char(sn.snap_time,'yyyy-mm-dd hh24') time_,
avg(newmen.value - oldmen.value) sorts_memeory,
avg(newdsk.value - olddsk.value) disk_sort
from stats$sysstat oldmen,
stats$sysstat newmen,
stats$sysstat newdsk,
stats$sysstat olddsk,
stats$snapshot sn
where newdsk.snap_id=sn.snap_id
and olddsk.snap_id=sn.snap_id-1
and newmen.snap_id=sn.snap_id
and newdsk.snap_id=sn.snap_id -1
and oldmen.name='sorts (memory)'
and newmen.name='sorts (memory)'
and olddsk.name='sorts (disk)'
and newdsk.name='sorts (disk)'
group by to_char(sn.snap_time,'yyyy-mm-dd hh24')
--执行最慢的前10个SQL???
select * from (
select
to_char(snap_time,'dd Mon HH24:mi:ss') mydate,
executions exec,
loads loads,
parse_calls parse,
disk_reads reads,
buffer_gets gets,
rows_processed rows_proc,
sorts sorts,
sql_text,
hash_value
from
perfstat.stats$sql_summary sql,
perfstat.stats$snapshot sn
where
sql.snap_id > 
(select min(snap_id) min_snap 
from stats$snapshot where snap_time > sysdate-$days_back)
and
sql.snap_id = sn.snap_id
order by $sortskey desc) tt where rownum<11;
--SQL缓存池的命中率查询(pinhitratio,gethitratio应该大于90%以上)

select namespace,gethitratio,pinhitratio,reloads,invalidations
from v$librarycache 
where namespace in ('SQL AREA','TABLE/PROCEDURE','BODY','TRIGGER')


--数据库的常规参数我就不说了,除了V$parameter中的常规参数外,ORACLE还有大量的隐含参数,下面的语句就可以查询到数据库的所有隐含参数以及其值与参数的描述。
SELECT NAME 
,VALUE 
,decode(isdefault, 'TRUE','Y','N') as "Default" 
,decode(ISEM,'TRUE','Y','N') as SesMod 
,decode(ISYM,'IMMEDIATE', 'I', 
'DEFERRED', 'D', 
'FALSE', 'N') as SysMod 
,decode(IMOD,'MODIFIED','U', 
'SYS_MODIFIED','S','N') as Modified 
,decode(IADJ,'TRUE','Y','N') as Adjusted 
,description 
FROM ( --GV$SYSTEM_PARAMETER 
SELECT x.inst_id as instance 
,x.indx+1 
,ksppinm as NAME 
,ksppity 
,ksppstvl as VALUE 
,ksppstdf as isdefault 
,decode(bitand(ksppiflg/256,1),1,'TRUE','FALSE') as ISEM 
,decode(bitand(ksppiflg/65536,3), 
1,'IMMEDIATE',2,'DEFERRED','FALSE') as ISYM 
,decode(bitand(ksppstvf,7),1,'MODIFIED','FALSE') as IMOD 
,decode(bitand(ksppstvf,2),2,'TRUE','FALSE') as IADJ 
,ksppdesc as DESCRIPTION 
FROM x$ksppi x 
,x$ksppsv y 
WHERE x.indx = y.indx 
AND substr(ksppinm,1,1) = '_' 
AND x.inst_id = USERENV('Instance') 

ORDER BY NAME 
--想知道现在哪个用户正在利用临时段吗?这个语句将告诉你哪个用户正在利用临时段。

SELECT b.tablespace, b.segfile#, b.segblk#, b.blocks, a.sid, a.serial#, 
a.username, a.osuser, a.status,c.sql_text 
FROM v$session a,v$sort_usage b, v$sql c 
WHERE a.saddr = b.session_addr 
AND a.sql_address = c.address(+) 
ORDER BY b.tablespace, b.segfile#, b.segblk#, b.blocks; 
--查看磁盘碎片 
select tablespace_name,sqrt(max(blocks)/sum(blocks))*
(100/sqrt(sqrt(count(blocks)))) FSFI
from dba_free_space
group by tablespace_name order by 1
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.查看回滚段名称及大小
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
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

16。查看锁(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 
--查看低效率的SQL语句
SELECT EXECUTIONS , DISK_READS, BUFFER_GETS,
ROUND((BUFFER_GETS-DISK_READS)/BUFFER_GETS,2) Hit_radio,
ROUND(DISK_READS/EXECUTIONS,2) Reads_per_run,
SQL_TEXT
FROM V$SQLAREA
WHERE EXECUTIONS>0
AND BUFFER_GETS > 0 
AND (BUFFER_GETS-DISK_READS)/BUFFER_GETS < 0.8 
ORDER BY 4 DESC

你可能感兴趣的:(dba)