本文是關(guān)于Oracle數(shù)據(jù)庫(kù)調(diào)試與優(yōu)化方面的文章,主要介紹Oracle數(shù)據(jù)庫(kù)中命中率相關(guān)的問題,包括不同的算法之間性能的比對(duì)。51CTO編輯向您推薦《Oracle數(shù)據(jù)庫(kù)調(diào)試與性能優(yōu)化》
關(guān)于Oracle中各個(gè)命中率的計(jì)算以及相關(guān)的調(diào)優(yōu)
1)Library Cache的命中率:
.計(jì)算公式:Library Cache Hit Ratio = sum(pinhits) / sum(pins)
SQL>SELECT SUM(pinhits)/sum(pins)
FROM V$LIBRARYCACHE;
通常在98%以上,否則,需要要考慮加大共享池,綁定變量,修改cursor_sharing等參數(shù)。
2)計(jì)算共享池內(nèi)存使用率:
SQL>SELECT (1 - ROUND(BYTES / (&TSP_IN_M * 1024 * 1024), 2)) * 100 || '%'
FROM V$SGASTAT
WHERE NAME = 'free memory' AND POOL = 'shared pool';
其中: &TSP_IN_M是你的總的共享池的SIZE(M)
共享池內(nèi)存使用率,應(yīng)該穩(wěn)定在75%-90%間,太小浪費(fèi)內(nèi)存,太大則內(nèi)存不足。
查詢空閑的共享池內(nèi)存:
SQL>SELECT * FROM V$SGASTAT
WHERE NAME = 'free memory' AND POOL = 'shared pool';
3)db buffer cache命中率:
計(jì)算公式:Hit ratio = 1 - [physical reads/(block gets + consistent gets)]
SQL>SELECT NAME, PHYSICAL_READS, DB_BLOCK_GETS, CONSISTENT_GETS,
1 - (PHYSICAL_READS / (DB_BLOCK_GETS + CONSISTENT_GETS)) "Hit Ratio"
FROM V$BUFFER_POOL_STATISTICS
WHERE NAME='DEFAULT';
通常應(yīng)在90%以上,否則,需要調(diào)整,加大DB_CACHE_SIZE
另外一種計(jì)算命中率的方法(摘自O(shè)RACLE官方文檔<<數(shù)據(jù)庫(kù)性能優(yōu)化>>):
命中率的計(jì)算公式為:
Hit Ratio = 1 - ((physical reads - physical reads direct - physical reads direct (lob)) / (db block gets +
consistent gets - physical reads direct - physical reads direct (lob))
分別代入上一查詢中的結(jié)果值,就得出了Buffer cache的命中率
SQL>SELECT NAME, VALUE
FROM V$SYSSTAT
WHERE NAME IN('session logical reads',
'physical reads',
'physical reads direct',
'physical reads direct (lob)',
'db block gets', 'consistent gets');
4)數(shù)據(jù)緩沖區(qū)命中率:
SQL> select value from v$sysstat where name ='physical reads';
SQL> select value from v$sysstat where name ='physical reads direct';
SQL> select value from v$sysstat where name ='physical reads direct (lob)';
SQL> select value from v$sysstat where name ='consistent gets';
SQL> select value from v$sysstat where name = 'db block gets';
這里命中率的計(jì)算應(yīng)該是
令 x = physical reads direct + physical reads direct (lob)
命中率 =100 - ( physical reads - x) / (consistent gets + db block gets - x)*100
通常如果發(fā)現(xiàn)命中率低于90%,則應(yīng)該調(diào)整應(yīng)用可可以考慮是否增大數(shù)據(jù)緩沖區(qū)
5)共享池的命中率:
SQL> select sum(pinhits-reloads)/sum(pins)*100 "hit radio" from v$librarycache;
假如共享池的命中率低于95%,就要考慮調(diào)整應(yīng)用(通常是沒使用bind var )或者增加內(nèi)存
6)計(jì)算在內(nèi)存中排序的比率:
SQL>SELECT * FROM v$sysstat t WHERE NAME='sorts (memory)';—查詢內(nèi)存排序數(shù)
SQL>SELECT * FROM v$sysstat t WHERE NAME='sorts (disk)';—查詢磁盤排序數(shù)
--caculate sort in memory ratio
SQL>SELECT round(&sort_in_memory/(&sort_in_memory+&sort_in_disk),4)*100||'%' FROM dual;
此比率越大越好,太小整要考慮調(diào)整,加大PGA
7)PGA的命中率:
計(jì)算公式:BP x 100 / (BP + EBP)
BP: bytes processed
EBP: extra bytes read/written
SQL>SELECT * FROM V$PGASTAT WHERE NAME='cache hit percentage';
或者從OEM的圖形界面中查看
我們可以查看一個(gè)視圖以獲取Oracle的建議值:
SQL>SELECT round(PGA_TARGET_FOR_ESTIMATE/1024/1024) target_mb,
ESTD_PGA_CACHE_HIT_PERCENTAGE cache_hit_perc,
ESTD_OVERALLOC_COUNT
FROM V$PGA_TARGET_ADVICE;
The output of this query might look like the following:
TARGET_MB CACHE_HIT_PERC ESTD_OVERALLOC_COUNT
---------- -------------- --------------------
63 23 367
125 24 30
250 30 3
375 39 0
500 58 0
600 59 0
700 59 0
800 60 0
900 60 0
在此例中:PGA至少要分配375M
我個(gè)人認(rèn)為PGA命中率不應(yīng)該低于50%
以下的SQL統(tǒng)計(jì)sql語(yǔ)句執(zhí)行在三種模式的次數(shù): optimal memory size, one-pass memory size, multi-pass memory size:
SQL>SELECT name profile, cnt, decode(total, 0, 0, round(cnt*100/total,4)) percentage
FROM (SELECT name, value cnt, (sum(value) over ()) total FROM V$SYSSTAT WHERE name like 'workarea exec%');
8)共享區(qū)字典緩存區(qū)命中率
計(jì)算公式:SUM(gets - getmisses - usage -fixed) / SUM(gets)
命中率應(yīng)大于0.85
SQL>select sum(gets-getmisses-usage-fixed)/sum(gets)
from v$rowcache;
9)數(shù)據(jù)高速緩存區(qū)命中率
計(jì)算公式:1-(physical reads / (db block gets + consistent gets))
命中率應(yīng)大于0.90最好
SQL>select name,value
from v$sysstat
where name in ('physical reads','db block gets','consistent gets');
10)共享區(qū)庫(kù)緩存區(qū)命中率
計(jì)算公式:SUM(pins - reloads) / SUM(pins)
命中率應(yīng)大于0.99
SQL>select sum(pins-reloads)/sum(pins)
from v$librarycache;
11)檢測(cè)回滾段的爭(zhēng)用
SUM(waits)值應(yīng)小于SUM(gets)值的1%
SQL>select sum(gets),sum(waits),sum(waits)/sum(gets)
from v$rollstat;
12)檢測(cè)回滾段收縮次數(shù)
SQL>select name,shrinks
from v$rollstat, v$rollname
where v$rollstat.usn = v$rollname.usn;
-----------------------------------------------------------------------------
幾個(gè)常用的檢查語(yǔ)句
1. 查找排序最多的SQL:
SQL>SELECT HASH_VALUE, SQL_TEXT, SORTS, EXECUTIONS
FROM V$SQLAREA
ORDER BY SORTS DESC;
2.查找磁盤讀寫最多的SQL:
SQL>SELECT * FROM
(SELECT sql_text,disk_reads "total disk" , executions "total exec",disk_reads/executions "disk/exec" FROM v$sql
WHERE executions>0 and is_obsolete='N' ORDER BY 4 desc)
WHERE ROWNUM<11 ;
3.查找工作量最大的SQL(實(shí)際上也是按磁盤讀寫來排序的):
SQL>select substr(to_char(s.pct, '99.00'), 2) || '%' load,s.executions executes,p.sql_text
from(select address,disk_reads,executions,pct,rank() over (order by disk_reads desc) ranking from
(select address,disk_reads,executions,100 * ratio_to_report(disk_reads) over () pct
from sys.v_$sql
where command_type != 47)
where disk_reads > 50 * executions) s,sys.v_$sqltext p
where s.ranking <= 5 and p.address = s.address
order by 1, s.address, p.piece;
4. 用下列SQL工具找出低效SQL:
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;
5、根據(jù)sid查看對(duì)應(yīng)連接正在運(yùn)行的sql
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);
***************Oracle 緩沖區(qū)命中率低的分析及解決辦法******************
首先確定下面的查詢結(jié)果:
1,緩沖區(qū)命中率的查詢(是否低于90%):
select round((1 - sum(decode(name,'physical reads',value,0)) /
(sum(decode(name,'db block gets',value,0)) + sum(decode(name,'consistent gets',value,0))) ),4) *100 || '%' chitrati
from v$sysstat;
2,使用率的查詢(有無free狀態(tài)的數(shù)據(jù)快.):
select count(*), status from v$bh group by status ;
3,相關(guān)等待事件的查詢(是否有相關(guān)等待事件)
select event,total_waits from v$system_event where event in ('free buffer waits');
4,當(dāng)前大小(是否已經(jīng)很大)
select value/1024/1024 cache_size from v$parameter where name='db_cache_size'
5,top等待事件分析(Db file scatered read的比率是否大)
select event ,total_waits,suml
from
(select event,total_waits,round(total_waits/sumt*100,2)||'%' suml
from
(select event,total_waits from v$system_event ),
(select sum(total_waits) sumt from v$system_event)
order by total_waits desc)
where rownum<6
and event not like 'rdbms%'
and event not like 'pmon%'
and event not like 'SQL*Net%'
and event not like 'smon%';
6,db_cache_advice建議值(9i后的新特性,可以根據(jù)他更好的調(diào)整cache_size)
select block_size,size_for_estimate,size_factor,estd_physical_reads from v$db_cache_advice;
說明分析:
緩沖區(qū)命中率(低于90的命中率就算比較低的).
沒有free不一定說明需要增加,還要結(jié)合當(dāng)前cache_size的大小(我們是否還可以再增大,是否有需要增加硬件,增加開銷),
空閑緩沖區(qū)等待說明進(jìn)程找不到空閑緩沖區(qū),并通過寫出灰緩沖區(qū),來加速數(shù)據(jù)庫(kù)寫入器生成空閑緩沖區(qū),當(dāng)DBWn將塊寫入磁盤后,灰數(shù)據(jù)緩沖區(qū)將被釋放,以便重新使用.產(chǎn)生這種原因主要是: