SQL Server clone database

Copyright: copy, please indicate the source https://blog.csdn.net/weixin_39392627/article/details/86853445

env:Windows Server 2016

       SQL Server 2016 SP2

       SQL Server 2016 SP1

Previously copied database in two ways, with a snapshot, use a backup file.

SQL Server 2012 provides a command to copy the database: dbcc clonedatabase.

This directive has gradually adding new parameters, you can refer to this link: the DBCC CLONEDATABASE (Transact-SQL)

We use SQL 2016 to test several functions.

Copy of about 200GB DB

 

1. Copy the database, with no arguments

command:

DBCC CLONEDATABASE (TESTDB01_20190119, TESTDB01_20190119_Clone);    
GO

Database replication process is very fast, and very little room to copy out the DB

 

Copy the default database is "read-only"

You can use the following command is changed to "read-write"

ALTER DATABASE UgsDataWarehouse_20190119_Clone SET READ_WRITE

 

2. You can check whether this database is to be copied out through the following instructions.

command:

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

 

3. Parameters "VERIFY_CLONEDB" is a new feature of SQL Server 2016 SP2

command:

DBCC CLONEDATABASE (TESTDB01_20190119, TESTDB01_20190119_Clone_1) WITH VERIFY_CLONEDB;    
GO

If the following error appears in the 2016 SP1 will execute SQL Server:

 

4. Try the SQL Server 2016 SP2 using the parameter "VERIFY_CLONEDB"

command:

DBCC CLONEDATABASE (TBTEST03, TBTEST03_CLONE) WITH VERIFY_CLONEDB;    
GO

 

errorlog content:

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.

Guess you like

Origin blog.csdn.net/weixin_39392627/article/details/86853445