📜 ⬆️ ⬇️

SQL101: Why restoring from a backup is slower than creating it

SQLskills launches new record placement initiative with basic knowledge, we called it SQL101. We will write about things that, as we often see, are done incorrectly, technologies that are used incorrectly, and many misunderstandings that lead to serious problems. If you want to find all the records in this series, check out the link SQLskills.com/help/SQL101 (English).

One of the questions that I constantly get asked is why it takes longer to restore a database from a full backup than to create a full backup. The answer is that the recovery process almost always requires more work.

Creating a full backup includes the following main stages:

  1. Create a checkpoint.
  2. Reading all the data used from the data files (technically, reading all the placed extents, no matter how many of the 8 pages in the extent are actually used).
  3. Reading the transaction log from the beginning of the oldest uncommitted transaction from the initial checkpoint to the moment when the second stage was completed. This is necessary so that the database can be restored to a consistent state at the point in time belonging to the backup period (see this article (English) for a detailed explanation).
  4. (Optionally testing the checksums of all pages, optionally performing backup compression and optionally encrypting the backup).

Recovery from a full backup includes the following main stages:
')
  1. Creating data files (and filling them with zeros if instant initialization of files is not allowed (English, a good alternative in Russian ).
  2. Copy data from backup to data files.
  3. Creating a transaction log file and filling it with zeros. The transaction log file must always be filled with zeros when it is created (see this article (English) for a detailed explanation).
  4. Copy transaction log from backup to log file.
  5. Starting a database crash recovery.
  6. (Optional testing of the checksums of all pages during the second phase, unpacking, if the backup was compressed, decryption, if the backup was encrypted.)

Stage 3 is often the longest stage in recovery, and it is proportional to the size of the transaction log. This process is carried out in a separate stage, instead of being performed in parallel with stage 1-2, and for in-depth study, see the recent entry in the Bob Ward blog .

Stage 5 can be the longest stage in the recovery process if there were long uncommitted transactions in the backup creation process. And it may be even longer if there are a large number of virtual log files (thousands) in the transaction log, since they greatly slow down the rollback mechanism of uncommitted transactions.

Here is a list of things you can do to ensure quick recovery from a full backup:


Hope this was helpful!

Source: https://habr.com/ru/post/329814/


All Articles