How to use DBCC CLONEDATABASE to generate a schema and statistics only copy of a user database in SQL Server 2014 SP2 and SQL Server 2016 SP1
About service packs for SQL Server
About DBCC CLONEDATABASE
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:
- Creates a new destination database that uses the same file layout as the source but with default file sizes as the model database.
- Creates an internal snapshot of the source database.
- Copies the system metadata from the source to the destination database.
- Copies all schema for all objects from the source to the destination database.
- Copies statistics for all indexes from the source to the destination database.
DBCC CLONEDATABASE (source_database_name, target_database_name)[WITH [NO_STATISTICS][,NO_QUERYSTORE]]
This argument is the name of the database whose schema and statistics need to be copied.
This argument is the name of the database which the schema and statistics from the source database will be copied to. This database will be created by DBCC CLONEDATABASE and should not already exist.
This argument specifies if table/index statistics needs to be excluded in the clone. If this option is not specified, table/index statistics are automatically included. This option is available starting with SQL Server 2016 Service Pack 1.
This argument specifies if query store needs to be excluded in the clone. If this option is not specified, query store data is copied to the clone if it is enabled in the source database. This option is available starting with SQL Server 2016 Service Pack 1.
When to use DBCC CLONEDATABASE?
The following validations are performed by DBCC CLONEDATABASE. The command fails if any of the validations fail.
- The source database must be a user database. Cloning of system databases (master, model, msdb, tempdb, distribution database etc.) isn't allowed.
- The source database must be online or readable.
- A database that uses the same name as the clone database must not already exist.
- The command isn't in a user transaction.
If all the validations succeed, DBCC CLONEDATABASE will run the following operations:
- Creating primary data file and log file
- Adding secondary dataspaces
- Adding secondary files
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.
- Internal Database snapshot
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:
- Validate the source database
- Get S lock for the source database
- Create snapshot of the source database
- Create a clone database (this is an empty database which inherits from model)
- 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.
- APPLICATION ROLE
- AVAILABILITY GROUP
- COLUMNSTORE INDEX
- CLR (only in SQL Server 2016 Service Pack 1 and later versions)
- DATABASE PROPERTIES
- FILES AND FILEGROUPS
- PARTITION FUNCTION
- PARTITION SCHEME
Note T-SQL procedures only for SQL Server 2014 SP2 and later CUs. Natively compiled stored procedures and CLR procedures won't be copied. In SQL Server 2016 Service Pack 1 and later versions, CLR and natively compiled procedures are also copied.
- QUERY STORE (only in SQL Server 2016 Service Pack 1 and later versions)
Note Query Store is copied only if it is enabled on the source database. To copy the latest runtime statistics as part of Query Store, execute sp_query_store_flush_db to flush the runtime statistics to the query store before executing DBCC CLONEDATABASE.
- SPATIAL INDEX
Note Only user and filestream tables are copied. Memory optimized tables and FileTables won't be copied in SQL Server 2014 Service Pack 2 and later CUs. In SQL Server 2016 Service Pack 1 and later CUs, Memory-optimized tables, Filestream, and FileTable objects are copied.
- UPGRADED DB
- XML INDEX
- XML SCHEMA COLLECTION
Error log messages
<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'.
Creating a clone of the AdventureWorks database that includes schema, statistics and query store (SQL Server 2016 SP1 and later versions)
Transact-SQL -- Generate the clone of AdventureWorks database. DBCC CLONEDATABASE (AdventureWorks, AdventureWorks_Clone); GO
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
Article ID: 3177838 - Last Review: 11/24/2016 02:45:00 - Revision: 5.0
- kbqfe kbfix kbsurveynew kbexpertiseadvanced KB3177838