調(diào)優(yōu)日志切換(Tuning Log Switches)
2024-07-21 02:05:48
供稿:網(wǎng)友
本文來源于網(wǎng)頁設計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。
調(diào)優(yōu)日志切換(tuning log switches)
日志切換:lgwr進程停止寫日志到當前日志文件,關閉日志文件,打開新的日志文件并寫日志緩存中的數(shù)據(jù)到新的日志文件。
日志切換可以命令alter system switch logfile或者alter system archive log來手工執(zhí)行,也可以通過設置參數(shù)log_archive_start使其自動執(zhí)行。一般的原因是一個進程不能將生成的重做日志從緩存中寫到當前的日志文件,因為已經(jīng)使用到當前日志文件的最后一個數(shù)據(jù)塊。
日志切換時做到的相關的步驟順序:
1、 執(zhí)行一個控制文件事務來選擇下一個日志文件以使用并清除控制文件入口。一般是選擇當前帶有最小日志序列號的日志文件來作為下一個使用,在這一步上,如果有必要,日志切換將會等待dbwr進程完全前面一個日志文件的日志切換的checkpoint,等待arcn進程完成歸檔。
2、 使用redo copy latches和redo generation latches兩個latch來阻止重做日志在日志緩存中生成(標記日志緩存狀態(tài)),并將日志緩存中的數(shù)據(jù)寫到日志文件(磁盤)中。如有可能用并行的方式,會同時將文件中最后一個記錄的scn號寫到頭數(shù)據(jù)塊中(header block)。在這些“寫”操作都完成之后lgwr進程關閉這個日志文件。
3、 提升scn值執(zhí)行第二個控制文件事務來將日志文件標記為current,把之前的日志文件標記為active,一旦dbwr進程完成了日志切換的checkpoint后,這個文件的狀態(tài)(之前的日志文件)會被標記為inactive。如果數(shù)據(jù)庫運行在歸檔模式下,lgwr進程通過控制文件中日志文件的入口段將這個日志文件(之前的日志文件)添加到歸檔連接單中,如果啟動的是自動歸檔,lgwr會激活arcn后臺進程將這個日志文件進行歸檔。如果當前使用的所有arcn進程都處于忙狀態(tài),lgwr會激活一個新的arcn進程,arcn進程數(shù)由參數(shù)log_archive_max_processes。
4、 最后一步,lgwr進程打開新日志文件組的所有成員,并將新的日志序列號和底scn寫到頭數(shù)據(jù)塊(header block)。然后將日志緩存狀態(tài)改為可以生成日志。
日志切換可能會花較長時間,經(jīng)常是以秒記,日志切換過程有較大的調(diào)優(yōu)潛力。相對于連續(xù)的緩慢的性能,oltp系統(tǒng)的用戶一般更難忍受數(shù)據(jù)庫的間斷的較差的性能,所以,減少日志切換頻率,減少切換間隔時間是非常重要的。
在系統(tǒng)性能方面,日志切換沖突的主要表現(xiàn)在于log file switch completion等待事件上。如果日志切換性能差,另外一個等待事件log buffer space等待也會即刻產(chǎn)生。對日志切換調(diào)優(yōu)就是要先調(diào)優(yōu)log file switch completion等待事件然后是log buffer space等待。
使用大的日志文件:為了最小化日志切換頻率,可以加大日志文件的size(盡可能大,最易歸檔),也要控制checkpoint(如設置參數(shù)fast_start_mttr_target)來使數(shù)據(jù)庫恢復性能最優(yōu)。
每個聯(lián)機日志文件應該放在專有的磁盤上以免arcn進程同時進入訪問該磁盤。不要使用小的日志文件來保存磁盤空間,因為保存下來的空間不會再被使用(聯(lián)機日志文件大小確定后是不會再增長的,所以不會用到您所保留下來的這些空間)
增加聯(lián)機日志文件會提高歸檔的間隔時間。、
另外一方面,在使用了大的聯(lián)機日志文件時,要通過調(diào)優(yōu)歸檔以減少資源使用強度。
保持日志文件是打開狀態(tài):調(diào)整日志切換的速度最大的調(diào)優(yōu)的潛在地方在于新日志文件成員的打開過程,打開新日志文件是用操作系統(tǒng)的指令open()。如果有其他的一個進程在同一個文件中已經(jīng)有打開文件的描述符則操作系統(tǒng)調(diào)用這個命令來打開新的日志文件的速度是很快的,主要原因是關于文件的一些信息都放在了系統(tǒng)的內(nèi)核內(nèi)存中。
下面這個腳本用于在實例啟動時將日志文件的信息放到內(nèi)核內(nèi)存中,可以在一定的程度上提高日志切換的速度。這個是shell腳本,用于unix操作系統(tǒng)中,用cron來定期每天實現(xiàn):hold_logs_open.sh
###############################################################################
#
# synopsis: hold_logs_open.sh instance
# purpose: to hold the log files open to accelerate log switches
#
# copyright: (c) ixora pty ltd
# author: steve adams with acknowledgements to chris bunting
#
###############################################################################
if [ $# -ne 1 ]
then
echo "usage: hold_logs_open instance" >&2
exit 1
fi
instance=$1
oracle_sid=$1
oraenv_ask=no
. oraenv
fd=3
echo '
set pages 0 feedback off
select member from v$logfile;
prompt end-of-files
' |
sqlplus -s internal |
while read logfile
do
if [ "$logfile" = end-of-files ]
then
if [ "$fd" -gt 3 ]
then
sleep 86460 &
fi
exit 0
fi
eval "exec $fd<$logfile"
fd=`expr $fd + 1`
#
# uncomment these lines if your shell only supports input redirection
# for single digit file descriptors.
#
# if [ "$fd" -eq 10 ]
# then
# sleep 86460 &
# fd=3
# fi
done
調(diào)整控制文件事務:每次日志切換都包含了兩次控制文件事務,控制文件事務是在cf隊列鎖的保護下執(zhí)行的,所以沒有進一步的必要來控制控制文件的“寫”操作,但是為了在控制文件事務期間的實例或者系統(tǒng)failure的可恢復性,還是有必要進行一定的控制。
控制文件的可恢復性,首先寫恢復結(jié)構(gòu)到控制文件的第二個數(shù)據(jù)塊,在寫控制文件中的目標塊時(控制文件事務所要寫的塊)等待其操作(恢復結(jié)構(gòu)寫操作)完成,因此,每個控制文件事務至少包含兩個寫i/o操作的等待。
如果使用的激活的控制文件是多個,則i/o操作會以串行的方式來實現(xiàn)。因此,要提高控制文件事務的性能就是要減少激活的控制文件數(shù)。在大多數(shù)情況下,可以只使用一個激活的控制文件,然后用硬件鏡像及使用命令alter system backup controlfile to trace來備份控制文件及保護控制文件。
下面的腳本(backup_controlfile.sql)提供了一種備份方案,主要在unix下實現(xiàn),將控制文件的備份放到數(shù)據(jù)庫的創(chuàng)建目錄中:
trace_file_name.sql(獲得跟蹤文件名)
-------------------------------------------------------------------------------
--
-- script: trace_file_name.sql
-- purpose: to get the name of the current trace file
--
-- copyright: (c) ixora pty ltd
-- author: steve adams
--
-- synopsis: @trace_file_name
--
-- or
--
-- set termout off
-- @trace_file_name
-- set termout on
-- ... &trace_name ...
--
-- description: this script gets the name of the trace file for the current
-- session. it can be used interactively, or from other scripts.
-- the name is saved in the sql*plus define &trace_name.
--
-- there are three versions of the query below, because the trace
-- files are named differently depending on the platform. the
-- two incorrect versions should be commented out or deleted.
--
-------------------------------------------------------------------------------
column trace_file_name new_value trace_name
column trace_file_zipped new_value trace_zipped noprint
select
d.value || '/ora_' || p.spid || '.trc' trace_file_name,
d.value || '/ora_' || p.spid || '.trc.gz' trace_file_zipped
from
( select
p.spid
from
sys.v_$mystat m,
sys.v_$session s,
sys.v_$process p
where
m.statistic# = 1 and
s.sid = m.sid and
p.addr = s.paddr
) p,
( select
value
from
sys.v_$parameter
where
name = 'user_dump_dest'
) d
/
select
d.value||'/'||lower(rtrim(i.instance, chr(0)))||'_ora_'||p.spid||'.trc' trace_file_name,
d.value||'/'||lower(rtrim(i.instance, chr(0)))||'_ora_'||p.spid||'.trc.gz' trace_file_zipped
from
( select
p.spid
from
sys.v_$mystat m,
sys.v_$session s,
sys.v_$process p
where
m.statistic# = 1 and
s.sid = m.sid and
p.addr = s.paddr
) p,
( select
t.instance
from
sys.v_$thread t,
sys.v_$parameter v
where
v.name = 'thread' and
(
v.value = 0 or
t.thread# = to_number(v.value)
)
) i,
( select
value
from
sys.v_$parameter
where
name = 'user_dump_dest'
) d
/
select
d.value || '/ora' || lpad(p.spid, 5, '0') || '.trc' trace_file_name,
d.value || '/ora' || lpad(p.spid, 5, '0') || '_trc.gz' trace_file_zipped
from
( select
p.spid
from
sys.v_$mystat m,
sys.v_$session s,
sys.v_$process p
where
m.statistic# = 1 and
s.sid = m.sid and
p.addr = s.paddr
) p,
( select
value
from
sys.v_$parameter
where
name = 'user_dump_dest'
) d
/
clear columns
backup_controlfile.sql
-------------------------------------------------------------------------------
-- script: backup_controlfile.sql
-- purpose: to save a create controlfile statement
--
-- copyright: (c) ixora pty ltd
-- author: steve adams
--
-- description: this script uses the backup controlfile to trace command to
-- save a create controlfile statement, and then moves the trace
-- file into the apt create directory.
--
-- the sql*plus connection is closed, because its trace file has
-- been moved.
-------------------------------------------------------------------------------
alter database backup controlfile to trace
/
set termout off
@trace_file_name
set termout on
disconnect
host mv &trace_name $create/create_controlfile.sql
exit
自www.ixor.com.au