This update introduces a new management command DBCC CLONEDATABASE (Transact-SQL) in Microsoft SQL Server 2014 Service Pack 2 and SQL Server 2016 SP1 . This command creates a new database that contains the schema of all the objects and statistics from the specified source database.
Note In SQL Server 2014 environments, you have to install Cumulative Update 3 for SQL Server 2014 SP2 to get the ability to clone Filestream, FileTable, and common language runtime (CLR) objects and to clone with NO_STATISTICS options by using DBCC.
About DBCC CLONEDATABASE
Microsoft Customer Support Services may ask you to generate a clone of a database by using DBCC CLONEDATABASE in order to investigate a performance issue related to the query optimizer.
Note The newly generated database generated from DBCC CLONEDATABASE isn't supported to be used as a production database and is primarily intended for troubleshooting and diagnostic purposes. We recommend detaching the cloned database after the database is created.
The cloning of the source database is performed by the following operations:
Syntax
| DBCC clonedatabase (source_database_name, target_database_name)[WITH [NO_STATISTICS] |
Arguments
When to use DBCC CLONEDATABASE?
DBCC CLONEDATABASE should be used to create a schema and statistics only copy of a production database in order to investigate query performance issues. Be aware of the following restrictions and supported objects:
If all the validations succeed, DBCC CLONEDATABASE will run the following operations:
Note All files in the target database will inherit the size and growth settings from the model database. File name convention: The file names for the destination database will follow the source_file_name _underscore_random number convention. If the generated file name already exists in the destination folder, DBCC CLONEDATABASE will fail.
DBCC CLONEDATABASE uses an internal database snapshot of the source database for the transactional consistency that is needed to perform the copy. This prevents blocking and concurrency problems when these commands are executed. If a snapshot cannot be created, DBCC CLONEDATABASE will fail. Database level locks are held during following steps of the copy process:
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.
Supported Objects
Only the following objects schema will be cloned in the destination database. Encrypted objects get cloned but aren’t supported in clone in this release of SQL Server. Any objects that are not listed in the following section aren't supported in the cloned:
Permissions
You have to have the membership in the sysadmin fixed server role.
Limitations and Considerations
DBCC CLONEDATABASE doesn’t support creation of a clone if there are any user objects (tables, indexes, schemas, roles, and so on) that were created in the model database. If user objects are present in the model database, the database clone fails with following error message:
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object <system table> with unique index 'index name'. The duplicate key value is <key value>
Error log messages
The following messages are logged in the error log during the cloning process:
<Timestamp> spid53 Database cloning for 'sourcedb' has started with target as 'sourcedb_clone'.
<Timestamp> spid53 Starting up database 'sourcedb_clone'.
<Timestamp> spid53 Setting database option TRUSTWORTHY to OFF for database 'sourcedb_clone'.
<Timestamp> spid53 Setting database option DB_CHAINING to OFF for database 'sourcedb_clone'.
<Timestamp> spid53 Starting up database 'sourcedb_clone'.
<Timestamp> spid53 Database 'sourcedb_clone' is a cloned database. A cloned database should be used for diagnostic purposes only and is not supported for use in a production environment.
<Timestamp> spid53 Database cloning for 'sourcedb' has finished. Cloned database is 'sourcedb_clone'.
Database Property
A new database property IsClone is added. DATABASEPROPERTYEX('dbname', 'IsClone') will return 1 if the database is generated by using DBCC CLONEDATABASE.
Examples
Creating a clone of the AdventureWorks database that includes schema, statistics and query store (SQL Server 2016 SP1 and later versions)
Transact-sql (adventureworks, adventureworks_clone); go |
Creating a schema-only clone of the AdventureWorks database in SQL Server 2014 without statistics (SQL Server 2014 SP2 CU3 and later versions)
| DBCC clonedatabase (adventureworks, adventureworks_clone) WITH no_statistics |
Creating a schema-only clone of the AdventureWorks database without statistics and query store (SQL Server 2016 SP1 and later versions)
| DBCC clonedatabase (adventureworks, adventureworks_clone) WITH no_statistics, no_querystore |
Years of Experience
Gratified Students
Training Batches
Training Hours
Please subscribe our technical blog to get recent updates.