Skip to main content
deleted 403 characters in body; edited title
Source Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663

How to speed Speed up restore database from snapshot in SQL Server?

To revert a test database to an initial state (after running a test), I would like to restore the database from snapshot. I'm using the following script to achieve that. 

However, the script execution now takes around 7-8 seconds, since it first disconnects all users from the database (by setting the DB to SINGLE_USERSINGLE_USER mode). Is

Is there any way how the restoration process could be made faster so that the script could be called ideally before each automated (E2E) test? Thank you for any opinions.

ALTER DATABASE [MyDb] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE [MyDb] FROM DATABASE_SNAPSHOT = @snapshotName;
ALTER DATABASE [MyDb] SET MULTI_USER;

EDIT [2121-10-16]Elapsed time for each command: I replaced the first ALTER DATABASE (setting the DB

  1. 3065 ms
  2. 2766 ms
  3. 2 ms

This technique is meant to single user mode) command with a new one which simply kills all sessions. It's much faster, I would say it's immediate (with no delay). Meaningbe used for end-to-end UI tests, that also the other ALTER DATABASE (setting the DB to multi user mode) commandso there is not neededno direct control over transactions during a test.

DECLARE @kill varchar(8000) = '';

SELECT @kill = @kill + 'KILL ' + CONVERT(varchar(5), session_id) + ';'
FROM sys.dm_exec_sessions
WHERE database_id  = db_id('MyDb');
PRINT "KILL commands to be executed: " + @kill

EXEC(@kill);

I would still be interested in if and howI'm running the actual RESTORE could be sped upSQL Server in Docker. Could that play a significant role in how long it takes?

How to speed up restore database from snapshot in SQL Server?

To revert a test database to an initial state (after running a test), I would like to restore the database from snapshot. I'm using the following script to achieve that. However, the script execution now takes around 7-8 seconds, since it first disconnects all users from the database (by setting the DB to SINGLE_USER mode). Is there any way how the restoration process could be made faster so that the script could be called ideally before each automated (E2E) test? Thank you for any opinions.

ALTER DATABASE [MyDb] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE [MyDb] FROM DATABASE_SNAPSHOT = @snapshotName;
ALTER DATABASE [MyDb] SET MULTI_USER;

EDIT [2121-10-16]: I replaced the first ALTER DATABASE (setting the DB to single user mode) command with a new one which simply kills all sessions. It's much faster, I would say it's immediate (with no delay). Meaning, that also the other ALTER DATABASE (setting the DB to multi user mode) command is not needed.

DECLARE @kill varchar(8000) = '';

SELECT @kill = @kill + 'KILL ' + CONVERT(varchar(5), session_id) + ';'
FROM sys.dm_exec_sessions
WHERE database_id  = db_id('MyDb');
PRINT "KILL commands to be executed: " + @kill

EXEC(@kill);

I would still be interested in if and how the actual RESTORE could be sped up.

Speed up restore database from snapshot

To revert a test database to an initial state (after running a test), I would like to restore the database from snapshot. I'm using the following script to achieve that. 

However, the script execution now takes around 7-8 seconds, since it first disconnects all users from the database (by setting the DB to SINGLE_USER mode).

Is there any way the restoration process could be made faster so that the script could be called ideally before each automated (E2E) test?

ALTER DATABASE [MyDb] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE [MyDb] FROM DATABASE_SNAPSHOT = @snapshotName;
ALTER DATABASE [MyDb] SET MULTI_USER;

Elapsed time for each command:

  1. 3065 ms
  2. 2766 ms
  3. 2 ms

This technique is meant to be used for end-to-end UI tests, so there is no direct control over transactions during a test.

I'm running the SQL Server in Docker. Could that play a significant role in how long it takes?

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Post Migrated Here from stackoverflow.com (revisions)
Source Link

How to speed up restore database from snapshot in SQL Server?

To revert a test database to an initial state (after running a test), I would like to restore the database from snapshot. I'm using the following script to achieve that. However, the script execution now takes around 7-8 seconds, since it first disconnects all users from the database (by setting the DB to SINGLE_USER mode). Is there any way how the restoration process could be made faster so that the script could be called ideally before each automated (E2E) test? Thank you for any opinions.

ALTER DATABASE [MyDb] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE [MyDb] FROM DATABASE_SNAPSHOT = @snapshotName;
ALTER DATABASE [MyDb] SET MULTI_USER;

EDIT [2121-10-16]: I replaced the first ALTER DATABASE (setting the DB to single user mode) command with a new one which simply kills all sessions. It's much faster, I would say it's immediate (with no delay). Meaning, that also the other ALTER DATABASE (setting the DB to multi user mode) command is not needed.

DECLARE @kill varchar(8000) = '';

SELECT @kill = @kill + 'KILL ' + CONVERT(varchar(5), session_id) + ';'
FROM sys.dm_exec_sessions
WHERE database_id  = db_id('MyDb');
PRINT "KILL commands to be executed: " + @kill

EXEC(@kill);

I would still be interested in if and how the actual RESTORE could be sped up.