SqlServer中tempdb的日志機(jī)制原理解析及示例分享
測試用例
我們分別在用戶數(shù)據(jù)庫(testpage),tempdb中創(chuàng)建相似對象t1,#t1,并在tempdb中創(chuàng)建創(chuàng)建非臨時表,然后執(zhí)行相應(yīng)的insert腳本(用以產(chǎn)生日志),并記錄執(zhí)行時間用以比較用以比較說明tempdb”快”
Code
用戶數(shù)據(jù)庫testpage
use testpage go create table t1 ( id int identity(1,1) not null, str1 char(8000) ) declare @t datetime2=sysutcdatetime() declare @i int set @i=1 while (@i<100000) begin insert into t1 select @i,'aa' select @i=@i+1 end select [extime]=DATEDIFF(S,@t,sysutcdatetime())
tempdb
use tempdb go create table #t1 ( id int not null, str1 char(8000) ) declare @t datetime2=sysutcdatetime() declare @i int set @i=1 while (@i<100000) begin insert into #t1 select @i,'aa' select @i=@i+1 end select [extime]=DATEDIFF(S,@t,sysutcdatetime())
非臨時表在tempdb中執(zhí)行
use tempdb go create table t1 ( id int not null, str1 char(8000) ) declare @t datetime2=sysutcdatetime() declare @i int set @i=1 while (@i<100000) begin insert into t1 select @i,'aa' select @i=@i+1 end select [extime]=DATEDIFF(S,@t,sysutcdatetime())
由圖1-1中我們可以看出,在普通表中執(zhí)行一分鐘的腳本,tempdb只需執(zhí)行22s.而普通表在tempdb中也只需27s均大大優(yōu)于普通表中執(zhí)行情況.
感興趣的朋友亦可在執(zhí)行過程中觀察日志相關(guān)的性能技術(shù)器的運(yùn)行情況如(Log Bytes Flusged \sec 等)
圖1-1
由此測試我們可以看出本文開始提到的”tempdb比其他數(shù)據(jù)庫快”.
實(shí)際并不是tempdb有什么魔法,而是tempdb的日志機(jī)制與其他數(shù)據(jù)庫大有不同.
Tempdb的日志機(jī)制
Tempdb Simple恢復(fù)模式(重啟后無需還原操作)
Tempdb使用最小化日志
Tempdb 不受系統(tǒng)CheckPoint影響(系統(tǒng)checkpoint不涉及tempdb,但人為tempdb中執(zhí)行會落盤)
Tempdb 在刷入數(shù)據(jù)頁到磁盤前,日志無需落盤(事務(wù)提交日志無需落盤)
"快"的原因
可以看到系統(tǒng)檢查點(diǎn)自身會繞過tempdb,tempdb執(zhí)行時無需日志先落盤.且會最小化日志記錄(關(guān)于此一個特性我會稍候陳述)這些都極大的緩解了磁盤IO瓶頸,使得tempdb相比其他DB會快很多.
注意:雖然系統(tǒng)checkpoint檢查點(diǎn)會繞過tempdb,但tempdb中人為執(zhí)行checkpoint還是會起作用,大家只應(yīng)測試環(huán)境中使用,正式環(huán)境中慎用!
在上面的實(shí)例中我們可以看到無論在表的類型是什么,在tempdb中速度都會有很大提升,但普通表的執(zhí)行時間還是略長于臨時表,這是因?yàn)槠胀ū淼牡娜罩居涗浶畔⑦€是要略多于臨時表的.
關(guān)于tempdb最小化日志
在堆表(heap)中 insert,update操作的的更新信息日志無需記錄.
我們通過簡單實(shí)例來看.
USE [tempdb] GO create table #nclst ( id int identity(1,1) primary key nonclustered,---heaptable str1 char(8000) ); create table #clst ( id int identity(1,1) primary key,------clustered str1 char(8000) ); checkpoint-----生產(chǎn)環(huán)境慎用! DBCC SHRINKFILE (N'templog' , 0, TRUNCATEONLY) GO insert into #nclst(str1) select 'aa' select [Current LSN],Operation,CONTEXT,[Log Record Length] from fn_dblog(null,null) where AllocUnitId is not null checkpoint-----生產(chǎn)環(huán)境慎用! DBCC SHRINKFILE (N'templog' , 0, TRUNCATEONLY) GO insert into #clst(str1) select 'aa' select [Current LSN],Operation,CONTEXT,[Log Record Length] from fn_dblog(null,null) where AllocUnitId is not null
由圖1-2中可以看出堆表中并未記錄Insert中的#ncls.str1的具體信息,而聚集表中則記錄相應(yīng)信息
圖1-2
Tempdb為何需要日志
既然tempdb每次重啟都會重新建立,我們無需重做日志,但運(yùn)行過程中是可能需要回滾的,這也是tempdb日志存在的原因.
Tempdb 不支持重做(Redo)但需支持回滾(rollback).
關(guān)于tempdb回滾.
Tempdb中如果日志文件中無足夠空間應(yīng)用回滾則會引起整個實(shí)例就宕機(jī)!
Tempdb最佳實(shí)踐-日志
a 不要tempdb中checkpoint(消耗巨大引起系統(tǒng)性能下滑)
b 不要tempdb中開啟過長事務(wù)(無法截?cái)嗳罩?造成日志過大,如回滾時無法回滾則宕機(jī))
c 一般需要中間表匹配的過程在tempdb中創(chuàng)建進(jìn)行(創(chuàng)建速度快,需視具體情況而定.)
d tempdb中使用堆表速度佳.(需視具體情況而定)
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。