Necessity Of Backing Up Tail Log When Restoring The Database

One of the features that have been incorporated with MS SQL Server 2005 and its newer versions is Tail-Log Backups, which is mainly associated with backup and restore of SQL Server databases using full or bulk-logged recovery models. In the article, we will be learning about the necessity of backing up Tail log when restoring the database of SQL Server.

What is Tail-Log Backup?

The Tail-Log Backups can be defined as the process to capture all the log records that have not been backed up (as the name suggests, the tail of the log). It is a feature that was introduced in SQL Server 2005 and its later versions. This form of log backup is mainly done to avoid loss of work and to make sure that the log chain is kept intact. It is necessary to have a backup of transaction log tail in order to start restore database operation to the point of failure. The tail-log backup will be the last backup of which one can use for the recovery purpose of database.

Though Tail-Log Backup proves to be quite beneficial in the cases stated above, there are situations that does not require Tail-log backup. They are:

  • There is no need of Tail-log backup if recovery point is already present in an earlier log backup
  • When the user needs to replace the database, that may include operations such as overwriting or moving database to different location
  • When User does not need to restore the database to a point of time after the latest backup of the database

Scenarios that Requires Tail-Log Backup During Restore

Here are some of the common scenarios where a tail-log backup is needed:

  1. When the database is Online
    • If the user wishes to perform restore operation of the online database, the first step is to back up the tail of the log. It is recommended to use ‘WITH RECOVERY’ option of the BACKUP T-SQL statement in order to avoid risk of having errors for the online database.
    • NORECOVERY can be used to back up the tail of the log and to keep the database in RESTORING state. It is useful when saving the tail log of the log before RESTORE operation or failing over to a secondary database.
    • User can use both NO_TRUNCATE and NORECOVERY options together to create a log backup, which will skip the log truncation and keep the database into RESTORING state in an atomic state
  2. When the database is Offline
  3. If the database is in offline state and fails to start, user needs to restore the database by creating backup of the tail of log. It is optional whether to use WITH NORECOVERY or not, as no transactions can occur during this time.

  4. When the database is damaged
  5. In case of database that is damaged, user can take tail-log backup with the help of WITHCONTINUE_AFTER_ERROR option of the BACKUP statement. Another option is to use CHECKSUM

Note: Backing up the tail-log on a damaged database will only succeed if the log files are in healthy state, the database is in the state that supports tail-log backups and no bulk-logged changes is present in the database. If the tail-log backup cannot be created, any transactions committed after the recent log backup will be lost. In such scenario you can take the help of third party SQL database repair to recover lost SQL database.

Tail-Log Backups with missing Backup Metadata

As discussed in the above section, the tail-log backups have the capability to capture the tail of the transaction log in case of the offline database, missing data or damaged files. It might lead to incomplete metadata from the restore information commands and msdb. Even though the metadata may be incomplete, the log that has been captured is complete and usable.

  • If the tail-log backup has incomplete metadata, the values of the columns of has_incomplete_metadata and HasIncompleteMetadata are set to 1.
  • If metadata in tail-log backup is incomplete, the backupfilegroup table will be missing most of the information related to filegroups during the tail-log backup. The table backupfilegroup will contain several columns that are having null values.

Conclusion

It can be concluded that there is a high necessity of backup up tail log when restoring the database in SQL Server. If the user does not backup the tail of the log, any transactions may be lost that has occurred since the last backup of the database. The article has discussed about tail log backup and the scenarios where backup of the tail log is needed for restoring the database.

Steps to Know How to Rebuild Index in SQL Server

What is Index in SQL Server?

In RDBMS, there is a data structure that is used to improve the performance of SQL server at time of retrieving any data and this data structure is known as database index in SQL server. Instead of searching each row in table, one can use indexes that will provide a way of locating data and hence, accessing the database table.

What is Index Rebuilding?

The process of recreating a new structure of index in SQL Server for maintaining health of database is known as Index Rebuilding. There is a Rebuild option in SQL Server through which one can recreate the database index. If index is disabled from database, then one can use Rebuild operation for returning back to its working state.

NOTE: If user interrupts/cancel the working of rebuilding operation, then all the modification made will be rolled back to its previous state.

Need to Rebuild Index in SQL Server

Generally, while performing changes or modification index fragmentation takes place in form of wasted memory (termed as Gaps in data pages) and logical fragmentation. Gaps in data pages result in reduction of rows, which are archived in cache of the server. On the other hand, logical fragmentation occupies the disk storage space with useless data. Therefore, the only way to prevent rows reduction and save disk storage space is to rebuild indexes in SQL server. This will highly boost up the speed and performance of the database.

When to Rebuild Indexes?

If one constantly rebuild index in SQL server, then it will effect the physical storage of machine or will result in any other hazardous situation. Therefore, it is important to learn that when to rebuild indexes.

  • If there is rapid index fragmentation, and user eagerly wants to execute Rebuild Index tasks with all other tasks related to database maintenance, then they can do so for boosting server performance.
  • For weekly maintaining database and boosting server performance, user can perform rebuilding operation once in a week. However, it would be risky if user do not rebuild index since a week.
  • Another condition is when user has not performed rebuilding from long time. In this condition, users will have to take following two alternatives into consideration:
    • Use Reorganize Index tasks with Update Statistics tasks
    • Use online version of Rebuild Index tasks

Measures to Rebuild Indexes

A very common question among SQL server users that How to rebuild index in SQL server? Well, by making use of Maintenance Plan Wizard of SQL server in following way, user can perform rebuild operation:

  • Launch the Maintenance Plan Wizard on your machine
    Maintenance Plan Wizard
  • Click on the scroll down button of Databases field & then select databases on which you want to perform rebuilding operation. You will have to select any of the displayed databases and then click on OK button to continue.
    select displayed databases
  • Now you will return back to previous wizard. In this wizard you will come across two options:
    two options
  • Object: This option is only enabled with some selective database. In other words, it will not be enabled with all the databases of the server. However, there are three options provided to you in this field i.e. Tables, Views, and Tables and Views. If you select Tables and Views option, then Rebuild operation will be applied to indexes related to tables and indexed views of the selected database. Well as per your need, you can also choose Tables or Views option.
    options
  • Selection: By clicking on selection drop-down button, you can choose that whether you want to perform rebuilding operation on all objects or selected objects. If you want to perform selective operation, then enable These objects option and select the objects that you want to take in operation; else activate All objects option. After finishing with all your selection procedure click on OK button.
    all objects or selected objects
  • Next come Free space options field in which you are provided with two options and by default ‘Reorganize pages with default amount of free space’ option is enabled. You can make use of any one of the two options to clean the Gaps in data pages.
    Free space options
  • Now comes an advance options field. With help these two option your will be able to determine that how much benefit will you have during rebuilding procedure.
    advance options field
  • The last step of rebuilding indexes in SQL server is to define a valid schedule that can run your Rebuild Index job.
    Rebuild Index job
  • Fill all the mandatory fields of this window with correct details and click on OK button.
    Job Schedule

Conclusion

How to rebuild index in SQL server is query that arises when user has to deal with index fragmentation problem. After going through whole blog, one can say that rebuilding index is an effective resource intensive tasks. In case, if the above workaround failed to rebuild indexes in SQL Server then you can also go with a SQL Recovery tool to fix the index fragmentation issues.

Usage of DBCC SQLPERF for Transaction Log Management

Problem

Transaction log is one of the main aspect of all databases. In some circumstances, the transaction logs get large and not know how much space is utilized by it, become a major issue. It effects the performance of SQL Server because of which user faces problem while working on it. So, how to resolve this issue to improve the SQL Server performance?

How to Use DBCC SQLPERF to Check Transaction Log Performance ?

User can overcome from the problem by checking the transaction log performance. They can use DBCC SQLPERF (logspace) syntax as it helps to give the information of size of transaction logs. It is used to manage various things such as it shows a list of all transaction logs as well the space that is used in transaction log. It clears the wait and latch stats. It also lists all the wait stats on SQL Server.

NOTE: Before using DBCC SQLPERF syntax, some permissions are required as mentioned:

  • To run this syntax on SQL Server, View Server State permission is required.
  • Alter Server State approval is needed to reset wait and latch statistics on SQL Server.
  • View Database State permission is required on SQL Database premium tiers.
  • SQL Database admin account is needed on Basic Tiers and SQL Database Standard.

There are some options which user can use to run DBCC SQLPERF syntax according to their requirement. Each option has its own importance as discussed below.

WAITSTATS

When there are some troubleshooting performance issue in SQL Server and it is difficult to understand from where the check. Then, it is recommended to look at high level on your SQL Server and after that find out the root cause. User can use the DBCC SQLPERF WAITSTATS; it gives the high-level information that is required to succeed. Each time a process, which is inside the SQL Server that has to wait for anything, a time that is spend on it is tracked. The wait could be tracked because of the delays in reading data, writing data or some external way. The waiting procedure is allocated a wait type. The wait types are not offensively descriptive. It requires a translation into something that is more meaningful. If the waits are reduced in SQL Server then, user can attain a better overall performance.

LOGSPACE

User can use DBCC SQLPERF LOGSPACE, as it helps to give the details of the Log Space that is used to increase or decrease on database. It gives the information of the used recovery models and transaction log backups that user is using. Even it also gives the details of the transaction logs for auto grow and auto shrink. Users can also check the size of file that has been increased or decreased. It helps to resolve the problem that is faced by them.

‘sys.dm_os_latch_stats’,CLEAR

DBCC SQLPERF (‘sys.dm_os_latch_stats’ clear) is lightweight synchronization mechanism, which defend the access to read as well as change in memory structure. It is held only for the duration of operation. It acts as the synchronization mechanism that helps to avoid the updation of two threads at the same time. However, if the thread requires a latch it will be moved from the running to suspended mode. Later on, it is placed on the wait list to await for the notification, which the latch has acquired in the mode of request.

‘sys.dm_os_wait_stats’,CLEAR

DBCC SQLPERF (sys.dm_os_wait_stats’ clear) offers a necessary metrics for analyzing SQL Server performance issues. Users can gathering all the information about the completed waits that are encountered at time of executing threads. It is a useful way to identify the issues such as waits on locks or IO latency issues. All the counters are reset when the SQL Server is restarted or when the commands are run.

Conclusion

In the above blog, a way to improve the SQL Server performance by using DBCC SQLPERF is discussed. There are various options being discussed that helps user to overcome from the problem. User can utilize any of the mentioned option to increase performance of SQL Server.

How to add new instance in SQL Server

  1. All program -> SQL Server Installation center
    In windows 8 -> Search -> SQL Server Installation Cepter
  2. From left menu select “Installation->New Sql server standalone installation or add features”
  3. Browse CD or Physical directory of SQL Server
  4. Auto run “Setup support rules” click ok
  5. Product update -> click next
  6. Install setup files -> click next
  7. Setup support rules and click next
  8. Select Installation type -> select “Perform new installation of SQL Server 2012” and click next1
  9. Enter product key and click next
  10. I accept the licence key
  11. Setup rules -> select “All features with default” -> Click next
  12. Feature selection -> Click Next
  13. Instance Configuration -> Named instance -> Type SQLExpress
    And InstanceId -> Type SQLEXPRESS and click next2
  14. Server configuration -> click next
  15. Database engine configuration -> Select windows authentication mode -> Click add current user and click next3
  16. Click next …. Next … next like as usual sql server installation.
  17. After complete installation. Try to enter using new created instance

4

SQL Join with Microsoft SQL Server

What is SQL Joins?

A SQL join clause is used to combine records from two or more tables in a database based on common field between them. It creates a set of rows in a temporary table.

There are different types of joins available in SQL:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
• SELF JOIN
• CARTESIAN JOIN/CROSS JOIN

To understand JOINs, let’s consider the following two tables Customers and Orders is as follows

Customers

01-Customer02

Orders

02-Orders02

INNER JOIN
INNER JOIN returns match data between tables. It returns rows when there is at least one match in both the tables.

03-InnerJoin

Syntax:

SELECT Cust.Id, Cust.Name, ord.Amount, ord.[Date]
FROM Customers Cust
INNER JOIN Orders ord
ON Cust.Id = ord.CustomerId;

Output:

Id          Name                                               Amount                                  Date

———– ————————————————– ————————————— ———-

2           Hamidur Rahman                          4000.00                                 2014-01-10

2           Hamidur Rahman                          5000.00                                 2014-01-10

3           Jamilur Rahman                            3000.00                                 2014-02-13

4           Hasan Sarwar                                  600.00                                  2014-03-22

LEFT JOIN/LEFT OUTER JOIN
Left Outer Join returns all the rows from the left table in conjunction with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values.

04-Left join

Syntax:

SELECT Cust.Id, Cust.Name, ord.Amount, ord.[Date]
FROM Customers Cust
LEFT JOIN Orders ord
ON Cust.Id = ord.CustomerId;

Output:

Id          Name                                               Amount                                  Date

———– ————————————————– ————————————— ———-

1           Mahedee Hasan                               NULL                                    NULL

2           Hamidur Rahman                           4000.00                                 2014-01-10

2           Hamidur Rahman                           5000.00                                 2014-01-10

3           Jamilur Rahman                              3000.00                                 2014-02-13

4           Hasan Sarwar                                  600.00                                  2014-03-22

5           Kamal Hossain                                NULL                                    NULL

6           Imran Khan                                     NULL                                    NULL

7           Abu Sayem                                       NULL                                    NULL

RIGHT JOIN/RIGHT OUTER JOIN
Right outer join returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.

05-Right join

Syntax:

SELECT Cust.Id, Cust.Name, ord.Amount, ord.[Date]
FROM Customers Cust
RIGHT JOIN Orders ord
ON Cust.Id = ord.CustomerId;

Output:

Id               Name                                               Amount                                  Date

———– ————————————————– ————————————— ———-

2                 Hamidur Rahman                        4000.00                                 2014-01-10

2                 Hamidur Rahman                        5000.00                                 2014-01-10

3                 Jamilur Rahman                           3000.00                                 2014-02-13

4                 Hasan Sarwar                                600.00                                  2014-03-22

NULL        NULL                                               800.00                                  2014-03-22

FULL JOIN /FULL OUTER JOIN
Full outer join is combines left outer join and right outer join. It returns row from either table when the conditions are met and returns null value when there is no match

06-Full Join

Syntax:

SELECT Cust.Id, Cust.Name, ord.Amount, ord.[Date]
FROM Customers Cust
FULL JOIN Orders ord
ON Cust.Id = ord.CustomerId;

Output:

Id                    Name                                               Amount                                  Date

———– ————————————————– ————————————— ———-

1                      Mahedee Hasan                             NULL                                    NULL

2                     Hamidur Rahman                          4000.00                                 2014-01-10

2                    Hamidur Rahman                           5000.00                                 2014-01-10

3                    Jamilur Rahman                             3000.00                                 2014-02-13

4                   Hasan Sarwar                                   600.00                                  2014-03-22

5                   Kamal Hossain                                 NULL                                    NULL

6                   Imran Khan                                      NULL                                    NULL

7                    Abu Sayem                                       NULL                                    NULL

NULL          NULL                                                800.00                                  2014-03-22

SELF JOIN
The SQL SELF JOIN is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.

Syntax:

SELECT A.Id, A.Name, B.Name AS Introducer
FROM Customers A, Customers B
WHERE A.IntroducerId = B.Id

Output:

Id          Name                                               Introducer

———– ————————————————– ————————————————–

1           Mahedee Hasan                             Hamidur Rahman

2           Hamidur Rahman                         Mahedee Hasan

3           Jamilur Rahman                           Hamidur Rahman

4           Hasan Sarwar                                Jamilur Rahman

5           Kamal Hossain                              Hasan Sarwar

6           Imran Khan                                   Kamal Hossain

7           Abu Sayem                                     Imran Khan

CARTESIAN JOIN/CROSS JOIN
The SQL cross join produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table, if no WHERE clause uses in the second table

07-crossjoin

Syntax:

SELECT cust.Id, cust.Name, ord.Amount, ord.[Date]
FROM Customers cust, Orders ord

SELECT cust.Id, cust.Name, ord.Amount, ord.[Date]
FROM Customers cust CROSS JOIN Orders ord

Output:

Id          Name                                               Amount                                  Date

———– ————————————————– ————————————— ———-

1           Mahedee Hasan                            4000.00                                 2014-01-10

2           Hamidur Rahman                       4000.00                                 2014-01-10

3           Jamilur Rahman                          4000.00                                 2014-01-10

4           Hasan Sarwar                                4000.00                                 2014-01-10

5           Kamal Hossain                             4000.00                                 2014-01-10

6           Imran Khan                                  4000.00                                 2014-01-10

7           Abu Sayem                                     4000.00                                 2014-01-10

1           Mahedee Hasan                            5000.00                                 2014-01-10

2           Hamidur Rahman                       5000.00                                 2014-01-10

3           Jamilur Rahman                          5000.00                                 2014-01-10

4           Hasan Sarwar                               5000.00                                 2014-01-10

5           Kamal Hossain                             5000.00                                 2014-01-10

6           Imran Khan                                  5000.00                                 2014-01-10

7           Abu Sayem                                    5000.00                                 2014-01-10

1           Mahedee Hasan                            3000.00                                 2014-02-13

2           Hamidur Rahman                       3000.00                                 2014-02-13

3           Jamilur Rahman                          3000.00                                 2014-02-13

4           Hasan Sarwar                               3000.00                                 2014-02-13

5           Kamal Hossain                             3000.00                                 2014-02-13

6           Imran Khan                                  3000.00                                 2014-02-13

7           Abu Sayem                                    3000.00                                 2014-02-13

1           Mahedee Hasan                            600.00                                  2014-03-22

2           Hamidur Rahman                       600.00                                  2014-03-22

3           Jamilur Rahman                          600.00                                  2014-03-22

4           Hasan Sarwar                               600.00                                  2014-03-22

5           Kamal Hossain                             600.00                                  2014-03-22

6           Imran Khan                                  600.00                                  2014-03-22

7           Abu Sayem                                    600.00                                  2014-03-22

1           Mahedee Hasan                            800.00                                  2014-03-22

2           Hamidur Rahman                        800.00                                  2014-03-22

3           Jamilur Rahman                          800.00                                  2014-03-22

4           Hasan Sarwar                               800.00                                  2014-03-22

5           Kamal Hossain                             800.00                                  2014-03-22

6           Imran Khan                                  800.00                                  2014-03-22

7           Abu Sayem                                    800.00                                  2014-03-22

Additional Information Related JOIN

Left Outer Join Where Null
Left outer join where null returns all the rows from the left table in conjunction without the matching rows from the right table.

08-left Join and Null

Syntax

SELECT Cust.Id, Cust.Name, ord.Amount, ord.[Date]
FROM Customers Cust
LEFT JOIN Orders ord
ON Cust.Id = ord.CustomerId
where ord.CustomerId is null

Output

Id          Name                                               Amount                                  Date

———– ————————————————– ————————————— ———-

1           Mahedee Hasan                              NULL                                    NULL

5           Kamal Hossain                               NULL                                    NULL

6           Imran Khan                                    NULL                                    NULL

7           Abu Sayem                                      NULL                                    NULL

Right Outer Join Where Null
Right outer join where null returns all the rows from the right table in conjunction without the matching rows from the left table.

08-Right Join and Null

Syntax:

SELECT Cust.Id, Cust.Name, ord.Amount, ord.[Date]
FROM Customers Cust
RIGHT JOIN Orders ord
ON Cust.Id = ord.CustomerId
where Cust.Id is null

Output

Id              Name          Amount        Date
———– ————————————————– ————————————— ———-
NULL       NULL          800.00        2014-03-22

Full Outer Join Where Null
Full outer join is combines left outer join and right outer join. It returns row from either table when the conditions are met and returns null value when there is no match

10-full Join and Null

Syntax

SELECT Cust.Id, Cust.Name, ord.Amount, ord.[Date]
FROM Customers Cust
FULL OUTER JOIN Orders ord
ON Cust.Id = ord.CustomerId
WHERE Cust.Id is null
OR ord.CustomerId is null

Output

Id          Name                                                  Amount                                  Date

———– ————————————————– ————————————— ———-

1           Mahedee Hasan                                  NULL                                    NULL

5           Kamal Hossain                                   NULL                                    NULL

6           Imran Khan                                        NULL                                    NULL

7           Abu Sayem                                          NULL                                    NULL

NULL        NULL                                            800.00                                  2014-03-22