筆者曾經在面試DBA時的一句”tempdb為什么比其他數據庫快?”使得95%以上的應試者都一臉茫然.Tempdb作為Sqlserver的重要特征,一直以來大家對它可能即熟悉又陌生.熟悉是我們時時刻刻都在用,陌生可能是很少有人關注它的運行機制.這次我將通過實例給大家介紹下tempdb的日志機制.
測試用例
我們分別在用戶數據庫(testpage),tempdb中創建相似對象t1,#t1,并在tempdb中創建創建非臨時表,然后執行相應的insert腳本(用以產生日志),并記錄執行時間用以比較用以比較說明tempdb”快”
Code
用戶數據庫testpage
use testpagegocreate table t1(id int identity(1,1) not null,str1 char(8000))declare @t datetime2=sysutcdatetime()declare @i intset @i=1while (@i<100000)begininsert into t1 select @i,'aa'select @i=@i+1endselect [extime]=DATEDIFF(S,@t,sysutcdatetime())
tempdb
use tempdbgocreate table #t1(id int not null,str1 char(8000))declare @t datetime2=sysutcdatetime()declare @i intset @i=1while (@i<100000)begininsert into #t1 select @i,'aa'select @i=@i+1endselect [extime]=DATEDIFF(S,@t,sysutcdatetime())
非臨時表在tempdb中執行
use tempdbgocreate table t1(id int not null,str1 char(8000))declare @t datetime2=sysutcdatetime()declare @i intset @i=1while (@i<100000)begininsert into t1 select @i,'aa'select @i=@i+1endselect [extime]=DATEDIFF(S,@t,sysutcdatetime())
由圖1-1中我們可以看出,在普通表中執行一分鐘的腳本,tempdb只需執行22s.而普通表在tempdb中也只需27s均大大優于普通表中執行情況.
感興趣的朋友亦可在執行過程中觀察日志相關的性能技術器的運行情況如(Log Bytes Flusged /sec 等)
新聞熱點
疑難解答