Sql Server 中數(shù)據(jù)庫在BULK_LOGGED/SIMPLE模式下的一些操作會(huì)采用最小化日志的記錄方式,以減小tran log落盤日志量從而提高整體性能.
這里我簡單介紹下哪些操作在什么樣的情況下會(huì)最小化日志記錄.以及現(xiàn)實(shí)生產(chǎn)環(huán)境中如何應(yīng)用最小化日志.
概念:SQL Server在滿足相應(yīng)條件的基礎(chǔ)上時(shí)進(jìn)行一些特定的操作如Rebuild Index時(shí)會(huì)進(jìn)行最小化Tran Log記錄操作,從而改善系統(tǒng)性能.
注意:含最小化操作日志操作段日志無法按時(shí)間點(diǎn)恢復(fù)(point in time)
需要還原模式為簡單或大容量日志
最小化日志的操作
Create Index,Alter Index Rebulid
Bulk import操作(BCP,Bulk insert)
Select into
Blob數(shù)據(jù)操作(使用Write等)
Insert select(sql 2008后特定條件下可以)
Merge(特定條件)
應(yīng)用:實(shí)際應(yīng)用過程中我們實(shí)際使用insert select的時(shí)候居多,就此介紹
關(guān)于insert select操作的最小化日志
聚集表
當(dāng)聚集表為空時(shí),使用TABLOCK 鎖提示將會(huì)最小化日志
當(dāng)聚集表非空時(shí),無論如何將不會(huì)最小化日志
非聚集表
當(dāng)堆表為空時(shí),使用TABLOCK鎖提示,表中行數(shù)據(jù),索引數(shù)據(jù)(非聚集索引)都會(huì)最小化日志
當(dāng)堆表非空時(shí),使用TABLOCK鎖提示,表中存在非聚集索引,則行數(shù)據(jù),索引數(shù)據(jù)均非最小化日志
注:最小化日志中表非復(fù)制表
一些文檔中在堆表有索引非空的情況認(rèn)為堆行數(shù)據(jù)會(huì)最小化日志,實(shí)際是錯(cuò)誤的.見圖b-2中說明
聚集表實(shí)例
聚集空最小化日志 圖a-1
create database testbulkgouse masterALTER DATABASE [testbulk] SET RECOVERY BULK_LOGGED WITH NO_WAITgouse testbulkgocreate table t1(id int not null identity (1,1),dystr varchar(200),fixstr char(500));goset nocount ondeclare @i intset @i=0while(@i<20000)begin insert into t1(dystr,fixstr)values('aaa'+str(RAND()*100000000),'bbb'+str(RAND()*100000000)) set @i=@i+1endcreate table tcls(id int ,dystr varchar(200),fixstr char(500))goCREATE UNIQUE CLUSTERED INDEX inx_id ON dbo.tcls (id)insert into dbo.tcls with(tablockx)select * from dbo.t1 ----cluster table emptyselect Operation,CONTEXT,[Log Record Length],AllocUnitName from fn_dblog(null,null)where AllocUnitName like '%tcls%'
新聞熱點(diǎn)
疑難解答
圖片精選