What Are The Backup Types In SQL Server

What Are The Backup Types In SQL Server

Backup is considered one of the biggest criteria in disaster recovery plans and strategy. Being a DBA, it is very important to take care of the database with backups all in place. A SQL Server database becomes complete with the data files, filegroups, and log files. Further, these files are inclusive of both user and system databases. Understanding the database backups can help us in better planning of disaster recovery strategy.

Let’s check a simple overview of backup types in SQL Server.

1. Full Backup

This is the most common backup type in SQL Server. A full backup pushes all the data and objects in the data files for a given database. The objects are nothing but the tables, views, procedures, functions, triggers, indexes, etc.

While other backups are little toiling to restore, the full backup becomes one of the simplest restoration processes. This is due to a single .bak file that comprises all the data including the log transactions.

2. Differential Backup

This is the second common backup that a DBA thinks of. A differential backup pushes all the data and objects for a given database that are modified since the last full backup. So basically, it means that the data modified after the recent full backup can rationally be smaller in size. However, the size of the differential backup is hypothetical and can vary depending on the number of changes done after the last full backup.

The differential backup file where the data is pushed is the .bak same as the full backup but consists of an additional statement of WITH DIFFERENTIAL in the backup script. The advantage of having a combination of full backup and differential backup strategy can reduce the risk of data loss. Additionally, they can even reduce the size of the backup files. Restoring a differential backup will mandatorily require the last full backup which will be considered a base.

3. Transaction Log Backup

Transaction log backup is one such backup that a DBA understands to restore a backup for a point-in-time recovery. The transaction log backup pushes all the log records into the .LDFfile since the last log backup. This backup type works only when the database is set to “Full” or “Bulk-logged” recovery model.

Once a transaction log backup is created, the .LDF logical file will be ready for reuse. In the production environment, the log backups are taken periodically to ensure point-in-time recovery for databases.

4. Full File Backup

Full file backup pushes all the data and objects to the specified data files or filegroups. This backup type is basically used lesser when compared to the other three backups mentioned above. Full file backup becomes a useful option when your database is big and has a huge data file.

5. Differential File Backup

Differential file backup pushes all the data and objects to the specified data files or filegroups that have changed since the last full file backups. So, if you are considering a differential backup with a huge size, then a differential file backup can be created. However, the differential file backup is considered not relevant as they are mostly related to just one data file.

6. Partial Backup

Partial backup is a backup type that pushes a complete writable portion of the database but excludes any read-only files or filegroups. The option was first introduced in SQL Server 2005. Additionally, there are chances where we may have read-only filegroups in our databases. So, this option of partial backup can help take the backup by excluding all the read-only filegroups in the database.

The backup type works only for full or differential backups but doesn’t work for transaction log backups. While a filegroup is changed from Read-Only to Read-Write, the backup will be taken automatically in the next Partial backup. However, while a filegroup is changed from Read-Write to Read-Only, a new filegroup should be created.

7. Differential Partial Backup

Differential partial backup pushes all the data and objects that have changed since the last partial backup. The feature was added in SQL Server 2005 and was designed for databases where the filegroups are broken up. A restore of the differential partial backup requires the last full partial backup. Like the full and differential backup scenario, the differential partial backup is smaller in size when compared to the partial backup. Due to which, the differential partial backups are always faster.

8. COPY ONLY Backup


A COPY ONLY backup is a backup type that works for either full or differential backup. The backup type with COPY ONLY option is designed to avoid any disruption to backup sequence for the respective database. Additionally, the backup option was introduced in SQL Server 2005. Therefore, the hassle of backup sequence disruption can be avoided using the COPY ONLY backup option. This scenario can be helpful when there are multiple Adhoc backups happening on a given database and the actual sequence is being interrupted.

What Is HADR_SYNC_COMMIT Wait Type In SQL Server

The HADR_SYNC_COMMIT wait type is a wait type that is commonly seen if your servers are configured with an AlwayOn recovery model. Further, the HADR_SYNC_COMMIT wait type was introduced in SQL Server 2012. If you have already worked on the Database Mirroring recovery model, then the wait type DBMIRROR_SEND in the database mirroring is relatively similar to as that of the HADR_SYNC_COMMIT wait type.

What is the HADR_SYNC_COMMIT Wait Type?

Before we start, one thing to get cleared is that, in AlwaysOn High-Availability, the transaction hit on a primary server first gets committed on the secondary and only then comes back to commit on primary.

HADR SYNC COMMIT Wait Type 1

The HADR_SYNC_COMMIT wait type represents the time taken by the secondary replica to acknowledge the harden log records to the primary replica. As soon as a transaction is started in the primary replica, the HADR_SYNC_COMMIT wait type starts. Once it starts, the transaction is moved to the secondary replica for hardening into the log. The HADR_SYNC_COMMIT wait time will only end when the secondary replica acknowledges that the transaction was committed on the secondary replica. This scenario happens when the AlwaysOn Availability Group is set to synchronous mode.

In a production environment, it is normal to experience HADR_SYNC_COMMIT wait type on your AlwaysOn Availability Group setup.

Reasons for HADR_SYNC_COMMIT Wait Types:

1. Intermittent network connection between primary and secondary replica.
2. 
Performance issues on storage subsystem on a secondary replica.
3. 
High log queue being transferred from primary to the secondary replica.

How to check a wait type with HADR_SYNC_COMMIT

SELECT *
FROM sys.dm_os_wait_stats
WHERE wait_type = 'HADR_SYNC_COMMIT'

From the code above, it is easy for you to figure out the wait types we see on primary as well as the secondary replica. I ran the above code on my primary replica and got the results as below:

HADR SYNC COMMIT Wait Type





From the figures, you may notice the waiting_tasks_count and wait_time_ms. Both columns represent the number of commands and the wait time respectively taken to commit on secondary and acknowledge on the primary.

How to check the average wait time using this data: 

HADR SYNC COMMIT Wait Type 3
From our gathered data, I am calculating the average wait time. Here, we may notice that the average wait time calculated is 8 ms which is a decent value.

HADR SYNC COMMIT Wait Type 4
 



Fix/Analysis: How to lower the HADR_SYNC_COMMIT wait type?

1. Change the Synchronous mode to Asynchronous mode of your secondary replica (this can completely remove the HADR_SYNC_COMMIT wait type)
2. 
Analyze the log send size, log send rate, redo queue size, redo rate, and mainly synchronization performance from AlwaysOn Availability Group dashboard.

Connect to your server à Expand “Always On High Availablity” à Right-click on your Availability Group à Click on “Show Dashboard” à Right-click on any column and view information

HADR SYNC COMMIT Wait Type 5

3. Troubleshoot using the DMV – sys.dm_hadr_database_replica_stats. This contains most of the information related to AlwaysOn. Additionally, you can check for other DMV too related to AlwaysOn. All of them start with a prefix as dm_hadr.
4. 
 Add counters on Availability Replica and check for performance-related issues trapped there.
5. 
Optimize your query (this is one of the foremost options to recommend to fellow DBAs and Database developers)

Conclusion

As the HADR_SYNC_COMMIT wait type is purely related to the performance of the secondary replica, it is always recommended checking the secondary replica initially. Try to optimize the query, lower the network intermittency, and avoid overload to the server to lower the wait type.

Note: In the production environment, it is recommended not to constantly switch between the synchronous and asynchronous mode. This could lead to data loss on disaster recovery situations. In case of emergence, perform the switch to Asynchronous, however, get them back to synchronous as early as possible. 

You also refer: "LCK_XXX Wait Types"

How To Clear IntelliSense Cache In SQL Server

While I was working with one of my DBA friends, we got into a topic wherein the SSMS was giving the “invalid object name” error even though the instance was correctly selected. Suddenly, the fix just ran into my mind. The error related to the database context can be due to the unrefreshed IntelliSense Cache. The IntelliSense Cache is not automatically refreshed in SQL Server. It is important to refresh the IntelliSense Cache manually.

The red underline of the database object that you have created may sometime freak you though you are connected to the correct database. This confusion is illustrated in a simple manner with a simple fix to clear the IntelliSense Cache.

Demonstration of a similar situation of the red line on the object names.

Step 1: Created a table as “Day” in one of my databases

Step 2: Dropped the table “Day”

Step 3: Trying to re-create the table with same name as “Day”. 

Clear IntelliSense Cache SQL Server

From the image, if you note that the object name shows a red underline with the tag as the object name already exists. This is purely the issue related to the IntelliSense Cache

To check IntelliSense Cache enabled or not in your SSMS.

Step 1: Click on “Query” option from Top Taskbar.  

Step 2: Check if the “IntelliSense Enable” option is highlighted as shown in the image. If the option is highlighted, which means the feature is enabled. 

Clear IntelliSense Cache SQL Server 2










Let’s check how to refresh the IntelliSense Cache manually. 

FIX 1: Through keyboard commands

Simply press Ctrl + Shift + R on your keyboard

FIX 2: Through GUI

From top menus bar, click on “Edit” à “IntelliSense” à “Refresh Local Cache”

Clear IntelliSense Cache SQL Server 3






How To Fix Invalid Object Name - Database Context - In SQL Server

 Well, this becomes the most used and seen error in SQL Server if you are working as a database developer or a DBA. Additionally, most of you must already be aware of the situation of how to react when you see the error as “Invalid object name”. But there are places where some of you may get hung on what next when you see the error.

The fix for the error – Invalid object name, due to database context is pretty straight forward. Most of the users who explore SQL Server initially will come to use the GUI more than using the script itself.

Here, there are chances that you might have opened a new instance that selects the master database by default but fetching the records of your desired kind. So, the user may just think of havoc in this situation.

For demonstration purposes, I have created a table as [testing] and trying to run in master. So, we are obvious here that we are going to get the error as – invalid object name, as we are in the wrong database. 

Fix Invalid Object Name SQL Server 1

Fix:

Immediately I changed the database name to the database where my table exists, the error is gone. 

Fix Invalid Object Name Database Context SQL Server 2

Always make sure to check the database context while opening a new database instance as they tend to open the master database by default. 

If the issue still persists, then it could be due to IntelliSense Cache not refreshed. Please click here for a fix.

For your reference: How To Fix - Invalid Object Name - String_Split 

How To Compress A Native Backup In SQL Server

 The compressions of backups in SQL Server is purely to improve the performance of the server itself. The idea behind a backup compression is to reduce the disc usage in your drive. I was working with one of my friends who is a recent explorer in SQL Server and wanted to gather information on backup compression. It would be a good time to fabricate the native backup to all my SQL friends.

The native backup compression feature has been in use right since the SQL Server start-point. However, the compression was available only in the Enterprise Edition of SQL Server 2008 or earlier. If you are using any of the SQL Server 2008 or older version, then the backup compression is going to work only on Enterprise Edition. Further, the backup compression feature is made available starting from SQL Server 2008 R2.

The usage of native backups in SQL Server is still optional and can be performed using a third-party too. While comparing the performance of the native backup with the third-party tool, the third-party tool performs much better.

However, let’s check on how to compress a native backup in SQL Server using GUI.

Step 1: Open the SSMS and Right-Click on your server.

Step 2: Navigate to Database Setting and check the Compress backup option

Compress Native Backup SQL Server 1

You are all set with enabling the native backups with compression. 

Once we are done with enabling the native backup compression option using GUI, we have another method to perform backup compression through the script. 

USE [master]
GO
BACKUP DATABASE [DBCompression_Testing]
TO DISK = N'D:\Backup\DBCompression_Test\NativeCompressionBackup.bak'
WITH COMPRESSION, STATS = 10
GO


Compress Native Backup In SQL Server 2

Using the script is simpler and more efficient than using the GUI. So personally, I prefer to run the backup through scripts than using GUI. However, both options are well enough to use.

For your reference:

How to create a full backup
How to create a differential backup
How to create a log backup