您好,登錄后才能下訂單哦!
當(dāng)truncate table發(fā)生時,如何進(jìn)行恢復(fù),相信大部分人都會選擇通過還原備份到truncate table前,然后將數(shù)據(jù)重新導(dǎo)入正式表中。
那么在SQL Server中是不是真的只有這種方法呢,當(dāng)然不是,這也是本文即將介紹的內(nèi)容,前提條件是SQL Server完整恢復(fù)模式(大容量日志模式未做測試,暫不介紹)。
首先,了解一下truncate table的一些相關(guān)知識
官檔:
TRUNCATE TABLE 通過釋放用于存儲表數(shù)據(jù)的數(shù)據(jù)頁刪除數(shù)據(jù),且僅在事務(wù)日志中記錄頁釋放。
這句話包含的信息量是很大的,通過實(shí)驗(yàn)進(jìn)行驗(yàn)證,解釋如下:
truncate table時,數(shù)據(jù)庫日志中不會記錄確切的已刪除值,只記錄截?cái)嘤涗浀捻摰腎D,并且這些記錄所占用的空間會被標(biāo)識為可重寫,
mdf中會暫時保留這些頁內(nèi)的數(shù)據(jù),當(dāng)有新事務(wù)寫入這些頁時,truncate table的數(shù)據(jù)將會被覆蓋(數(shù)據(jù)頁被format,然后重新使用)。
測試:
創(chuàng)建一張表,并插入數(shù)據(jù)
create table test_truncate(id int,name varchar(20),address varchar(20)) go insert into test_truncate select 1,'zhangsan','first road' go insert into test_truncate select 2,'wangxiao','second road' go
利用dbcc ind找到該表的數(shù)據(jù)頁,如下PageType=1為數(shù)據(jù)頁,即為:288
使用dbcc page查看數(shù)據(jù)頁內(nèi)容
PAGE: (1:288) BUFFER: BUF @0x000000000563C600 bpage = 0x0000000150020000 bhash = 0x0000000000000000 bpageno = (1:288) bdbid = 9 breferences = 0 bcputicks = 0 bsampleCount = 0 bUse1 = 56673 bstat = 0x10b blog = 0x7adb21cc bnext = 0x0000000000000000 PAGE HEADER: Page @0x0000000150020000 m_pageId = (1:288) m_headerVersion = 1 m_type = 1 m_typeFlagBits = 0x0 m_level = 0 m_flagBits = 0x8000 m_objId (AllocUnitId.idObj) = 489 m_indexId (AllocUnitId.idInd) = 256 Metadata: AllocUnitId = 72057594069975040 Metadata: PartitionId = 72057594062241792 Metadata: IndexId = 0 Metadata: ObjectId = 935674381 m_prevPage = (0:0) m_nextPage = (0:0) pminlen = 8 m_slotCnt = 2 m_freeCnt = 8021 m_freeData = 167 m_reservedCnt = 0 m_lsn = (49:7380:2) m_xactReserved = 0 m_xdesId = (0:0) m_ghostRecCnt = 0 m_tornBits = 0 DB Frag ID = 1 Allocation Status GAM (1:2) = ALLOCATED SGAM (1:3) = ALLOCATED PFS (1:1) = 0x61 MIXED_EXT ALLOCATED 50_PCT_FULL DIFF (1:6) = CHANGED ML (1:7) = NOT MIN_LOGGED Slot 0 Offset 0x60 Length 35 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS Record Size = 35 Memory Dump @0x000000006DDF8060 0000000000000000: 30000800 01000000 03000002 00190023 007a6861 0..............#.zha 0000000000000014: 6e677361 6e666972 73742072 6f6164 ngsanfirst road Slot 0 Column 1 Offset 0x4 Length 4 Length (physical) 4 id = 1 Slot 0 Column 2 Offset 0x11 Length 8 Length (physical) 8 name = zhangsan Slot 0 Column 3 Offset 0x19 Length 10 Length (physical) 10 address = first road Slot 1 Offset 0x83 Length 36 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS Record Size = 36 Memory Dump @0x000000006DDF8083 0000000000000000: 30000800 02000000 03000002 00190024 0077616e 0..............$.wan 0000000000000014: 67786961 6f736563 6f6e6420 726f6164 gxiaosecond road Slot 1 Column 1 Offset 0x4 Length 4 Length (physical) 4 id = 2 Slot 1 Column 2 Offset 0x11 Length 8 Length (physical) 8 name = wangxiao Slot 1 Column 3 Offset 0x19 Length 11 Length (physical) 11 address = second road DBCC execution completed. If DBCC printed error messages, contact your system administrator.
可以看到數(shù)據(jù)頁中存在的記錄
Slot 0 Column 1 Offset 0x4 Length 4 Length (physical) 4 id = 1 Slot 0 Column 2 Offset 0x11 Length 8 Length (physical) 8 name = zhangsan Slot 0 Column 3 Offset 0x19 Length 10 Length (physical) 10 address = first road Slot 1 Column 1 Offset 0x4 Length 4 Length (physical) 4 id = 2 Slot 1 Column 2 Offset 0x11 Length 8 Length (physical) 8 name = wangxiao Slot 1 Column 3 Offset 0x19 Length 11 Length (physical) 11 address = second road
執(zhí)行truncate table后
可以看到數(shù)據(jù)頁中仍保留著truncate table的相關(guān)記錄。
那么,truncate table的第二種恢復(fù)方法就顯而易見了,在這些數(shù)據(jù)被覆蓋之前,從頁面中將數(shù)據(jù)提取出來,并恢復(fù)到表中。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。