SQL Server clone database

版权声明:複製請註明來源 https://blog.csdn.net/weixin_39392627/article/details/86853445

env:Windows Server 2016

       SQL Server 2016 SP2

       SQL Server 2016 SP1

以前複製資料庫有2種方式,一個用snapshot,一個使用備份檔。

SQL Server 2012提供一個複製資料庫的指令:dbcc clonedatabase。

這個指令陸續都有增加新的參數,可以參考此連結:DBCC CLONEDATABASE (Transact-SQL)

我們使用SQL 2016來測試幾個功能。

複製的DB約有200GB

 

1.進行複製資料庫,不帶參數

command:

DBCC CLONEDATABASE (TESTDB01_20190119, TESTDB01_20190119_Clone);    
GO

複製資料庫的過程十分快速,而且複製出來的DB空間很小

 

複製的資料庫預設是"read-only"

可以利用以下指令變更為"read-write"

ALTER DATABASE UgsDataWarehouse_20190119_Clone SET READ_WRITE

 

2.可以透過以下指令檢查此資料庫是否為被複製出來的。

command:

USE [master]
GO
SELECT DATABASEPROPERTYEX('TESTDB01_20190119_Clone', 'IsClone')
GO

 

3.參數"VERIFY_CLONEDB"是SQL Server 2016 SP2的新功能

command:

DBCC CLONEDATABASE (TESTDB01_20190119, TESTDB01_20190119_Clone_1) WITH VERIFY_CLONEDB;    
GO

假如在SQL Server 2016 SP1執行會出現以下錯誤:

 

4.嘗試在SQL Server 2016 SP2使用參數"VERIFY_CLONEDB"

command:

DBCC CLONEDATABASE (TBTEST03, TBTEST03_CLONE) WITH VERIFY_CLONEDB;    
GO

 

errorlog內容:

DBCC CHECKDB (TBTEST03_CLONE) WITH no_infomsgs executed by sa found 0 errors and repaired 0 errors. Elapsed time: 0 hours 0 minutes 0 seconds.  Internal database snapshot has split point LSN = 00000022:00000262:0001 and first LSN = 00000022:00000260:0001

 

5.檢查複製的資料庫在過程中是否有被verify

command:

USE [master]
GO
SELECT DATABASEPROPERTYEX('TBTEST03_CLONE', 'IsVerifiedClone')
GO

 

6.在複製指令中加入參數"VERIFY_CLONEDB"與"BACKUP_CLONEDB"

"BACKUP_CLONEDB"這個參數會在複製資料庫後進行備份,這個功能是在SQL Server 2016 SP2開始

command:

DBCC CLONEDATABASE (TBTEST03, TBTEST03_CLONE_1) WITH VERIFY_CLONEDB, BACKUP_CLONEDB;    
GO

 

errorlog內容,時間由近到遠:

CHECKDB for database 'TBTEST03_CLONE_1' finished without errors on 2019-01-19 09:40:35.853 (local time). This is an informational message only; no user action is required.

Database backed up. Database: TBTEST03_CLONE_1, creation date(time): 2019/01/19(09:40:34), pages dumped: 354, first LSN: 34:576:37, last LSN: 34:595:1, number of dump devices: 1, device information: (FILE=1, TYPE=DISK: {'E:\DBBackup\TBTEST03_CLONE_1_1176299034.bak'}). This is an informational message only. No user action is required.

CHECKDB for database 'TBTEST03_CLONE_1' finished without errors on 2019-01-19 09:40:35.853 (local time). This is an informational message only; no user action is required.

CHECKDB for database 'TBTEST03_CLONE_1' finished without errors on 2019-01-19 09:40:35.853 (local time). This is an informational message only; no user action is required.

DBCC CHECKDB (TBTEST03_CLONE_1) WITH no_infomsgs executed by sa found 0 errors and repaired 0 errors. Elapsed time: 0 hours 0 minutes 0 seconds.  Internal database snapshot has split point LSN = 00000022:00000233:0001 and first LSN = 00000022:00000231:0001.

DBCC CHECKDB (TBTEST03_CLONE_1) WITH no_infomsgs executed by sa found 0 errors and repaired 0 errors. Elapsed time: 0 hours 0 minutes 0 seconds.  Internal database snapshot has split point LSN = 00000022:00000233:0001 and first LSN = 00000022:00000231:0001.

Database 'TBTEST03_CLONE_1' is a cloned database. This database should be used for diagnostic purposes only and is not supported for use in a production environment.

 

結論:

這個功能方便使用,但是官方也提供以下警語。

這個是使用資料庫內部的快照功能來進行複製所以還是會產生LOCK。若要在生產環境使用,還是請再三評估。

 

DBCC CLONEDATABASE uses an internal database snapshot of the source database for the transactional consistency that is needed to perform the copy. Using this snapshot prevents blocking and concurrency problems when these commands are executed. If a snapshot can't be created, DBCC CLONEDATABASE will fail.

Database level locks are held during following steps of the copy process:

Validate the source database

Get S lock for the source database

Create snapshot of the source database

Create a clone database (an empty database inherited from the model database)

Get X lock for the clone database

Copy the metadata to the clone database

Release all DB locks

As soon as the command has finished running, the internal snapshot is dropped. TRUSTWORTHY and DB_CHAINING options are turned off on a cloned database.

猜你喜欢

转载自blog.csdn.net/weixin_39392627/article/details/86853445