亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SqlServer中tempdb的日志機(jī)制原理解析及示例分享

 更新時(shí)間:2014年08月20日 08:58:16   投稿:hebedich  
tempdb為什么比其他數(shù)據(jù)庫快?估計(jì)95%以上的程序員們都一臉茫然.Tempdb作為Sqlserver的重要特征,一直以來大家對(duì)它可能即熟悉又陌生.熟悉是我們時(shí)時(shí)刻刻都在用,陌生可能是很少有人關(guān)注它的運(yùn)行機(jī)制.這次我將通過實(shí)例給大家介紹下tempdb的日志機(jī)制.

測(cè)試用例

我們分別在用戶數(shù)據(jù)庫(testpage),tempdb中創(chuàng)建相似對(duì)象t1,#t1,并在tempdb中創(chuàng)建創(chuàng)建非臨時(shí)表,然后執(zhí)行相應(yīng)的insert腳本(用以產(chǎn)生日志),并記錄執(zhí)行時(shí)間用以比較用以比較說明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())

非臨時(shí)表在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

由此測(cè)試我們可以看出本文開始提到的”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í)行會(huì)落盤)

Tempdb 在刷入數(shù)據(jù)頁到磁盤前,日志無需落盤(事務(wù)提交日志無需落盤)

"快"的原因

可以看到系統(tǒng)檢查點(diǎn)自身會(huì)繞過tempdb,tempdb執(zhí)行時(shí)無需日志先落盤.且會(huì)最小化日志記錄(關(guān)于此一個(gè)特性我會(huì)稍候陳述)這些都極大的緩解了磁盤IO瓶頸,使得tempdb相比其他DB會(huì)快很多.

注意:雖然系統(tǒng)checkpoint檢查點(diǎn)會(huì)繞過tempdb,但tempdb中人為執(zhí)行checkpoint還是會(huì)起作用,大家只應(yīng)測(cè)試環(huán)境中使用,正式環(huán)境中慎用!

在上面的實(shí)例中我們可以看到無論在表的類型是什么,在tempdb中速度都會(huì)有很大提升,但普通表的執(zhí)行時(shí)間還是略長(zhǎng)于臨時(shí)表,這是因?yàn)槠胀ū淼牡娜罩居涗浶畔⑦€是要略多于臨時(shí)表的.

關(guān)于tempdb最小化日志

在堆表(heap)中 insert,update操作的的更新信息日志無需記錄.

我們通過簡(jiǎn)單實(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每次重啟都會(huì)重新建立,我們無需重做日志,但運(yùn)行過程中是可能需要回滾的,這也是tempdb日志存在的原因.

Tempdb 不支持重做(Redo)但需支持回滾(rollback).

關(guān)于tempdb回滾.

Tempdb中如果日志文件中無足夠空間應(yīng)用回滾則會(huì)引起整個(gè)實(shí)例就宕機(jī)!

Tempdb最佳實(shí)踐-日志

a 不要tempdb中checkpoint(消耗巨大引起系統(tǒng)性能下滑)

b 不要tempdb中開啟過長(zhǎng)事務(wù)(無法截?cái)嗳罩?造成日志過大,如回滾時(shí)無法回滾則宕機(jī))

c 一般需要中間表匹配的過程在tempdb中創(chuàng)建進(jìn)行(創(chuàng)建速度快,需視具體情況而定.)

d tempdb中使用堆表速度佳.(需視具體情況而定)

相關(guān)文章

最新評(píng)論