使用DBCC SHRINKFILE EMPTYFILE 選項遷移數據

對於DBCC SHRINKFILE EMPTYFILE的解釋:

 

將指定文件中的所有數據遷移到同一文件組中的其他文件。由於數據庫引擎不再允許將數據放在空文件內,因此可以使用ALTERDATABASE語句來刪除該文件。

 

假設說我現在想將數據從一個磁盤移動到另外一個磁盤,在移動過程中不想數據庫Offline,我們可以使用這個選項。下面是一個例子:

 

--create demodatabase

createdatabase test

onprimary( name =test,filename='D:\testdata\test_primary.mdf'),

filegroup [seconday]

(name = testsecondary,filename='d:\testdata\test_secondary.ndf')

logon (name = test_log,filename='d:\testdata\test_log.ldf')

 

 

--create tableon secondary filegroup

use test

go

createtable test(id int)on [seconday]

 

--Insert Demodata

 

declare @int int

set @int =0

while @int <100000

begin

insertinto test values (@int )

set @int = @int+1

end

--Add another dadtafile on secondary file group

alterdatabase test 

addfile

(name = test_secondary_new,filename='d:\testdata\test_secondary_new.ndf')

to filegroup[seconday]

--Empty oldfile and data will move to another file in the same filegroup

dbccshrinkfile('testsecondary',emptyfile)

go

--Show filesizeafter empty file

dbccshowfilestats

go

--remove old file

alterdatabase test remove filetestsecondary

--drop demodatabase

usemaster

go

dropdatabase test

 

使用這個選項不能夠移動系統對象,所以有侷限性。另外性能上來講肯定沒有detach然後附近來的快,好處是整個數據庫不會offline.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章