5/17/2018

Query performance approach in SQL Server - Simplest way to improve query performance - Part 1


Simplest way to tune SQL Query Part 1

If your query is running slow than you can use following ways to improve its performance. These are my real time scenarios where I got success by implementing these methods to improve performance of the query.
Method 1: Avoid “Select * from”, use column name i.e. Select Column1, Column2…:

To fetch the result suppose if you are having 10 columns in your table and you need only 5 out of 10 for your result than use all 5 columns name only separated by comma in Select statement instead of using as:
Correct Method:

Select Emp.FirstName, Emp.LastName, Emp.Salary, Dept.DeptName

FROM #Employee As Emp

LEFT JOIN #Dept As Dept  ON Emp.DeptID=Dept.DeptID
Incorrect Method:

SELECT * FROM #Employee As Emp

LEFT JOIN #Dept As Dept  ON Emp.DeptID=Dept.DeptID

Method 2: Use Key columns (Index columns) for filter and join:
If you have indexes in your tables which you are querying or joining, then try to use those columns if possible for joining condition and use in where clause to filter data, this will increase performance of your query.

Method 3: Never use Cast or convert in Where clause and in Join condition:
Suppose if you have Date filter in your table and if you are using Cast or Convert operator while filtering your data than you should avoid it. It will make your query much slower. And same for Join condition also.

Method 4: Use CTE result set for big table or expression based table to join with other table:
Select Column1, Column2,.....ColumnN

From TableA

left join TableB  on TableA.ColumnN = TableB.ColumnN     

left join TableC  on TableB.ColumnN = TableC.ColumnN

left join TableD  on TableC.ColumnN = TableD.ColumnN

Where TableA.Column1 = 0 and TableA.Date >= '01/01/2018'

As per given above query example; we have four  tables to join with each other based on conditions so first table here is a big table where we have millions records and another table also big one. But you can see in Where clause, we have filters for TableA only so If we run this query, than it will take time to give output so we can re-write in an efficient way with CTE as:

WITH TableACte

(

Select Column1, Column2,.....ColumnN

From TableA

Where TableA.Column1 = 0 and TableA.Date >= '01/01/2018'

)

Select Column1, Column2,.....ColumnN

From TableACte

left join TableB  on TableACte.ColumnN = TableB.ColumnN     

left join TableC  on TableB.ColumnN = TableC.ColumnN

left join TableD  on TableC.ColumnN = TableD.ColumnN

NOTE: In my Scenario, My query was taking 12 minutes to execute and now with this approach, it is giving result in less than 1 minute as Data is in billions.

Method 5: Avoid Case or any expression i.e. If condition in Where and Join condition
If you are using Case or If condition in Where clause or Join condition in your query than it will make your query very slow if data is huge because for each row it will calculate condition and will apply for filter in Where clause and join condition. So find alternate ways to do same or you can use sub query to build derived column.

Method 6: Try to use non-text columns for where clause and Join condition, use only Key columns where Datatype is not varchar() or nvarchar().
Always best method to use non text columns for join condition and if possible for Where clause also for filtering data because Text value will take time to get filter and join data.

Stay Tune for more methods in next part....
 

No comments:

Post a Comment

Tableau interview questions and answers for experienced professionals

  Tableau Interview Questions and Answers for experienced professional 1. What is TABLEAU? Tableau  is the powerful and fastest visualizing ...