五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動(dòng)態(tài)

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

發(fā)布日期:2021-12-26 17:57 | 文章來源:gibhub

測(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中使用堆表速度佳.(需視具體情況而定)

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部