SQL query optimization tips and tricks

Optimized SQL query writing

Tips and Tricks

       1. Remove * (Star) from queries

Try not to use *(Star) short hand syntax in SELECT statement specially when a table containing large column list.

What SQL do internally?

If we use *(Star) then SQL will replace the star with column name at runtime but if we do not use *(star) then SQL will not need to do this extra work which saves a little time. You can check out the SQL Keywords Reference for a better understanding.

Screenshot of source code snippet displaying if we use of *(Star) then SQL will replace the star with column name at runtime.

But if we need all the column then it is better to write * (star) as compared to the time it saves

   2. Remove Unnecessary columns from a query

Usually we do not need all the column in the UI/ Client side. Just remove the unnecessary columns from SELECT Statements.

Screenshot of source code snippet displaying removal of the unnecessary columns from SELECT Statements.

       3. Try to avoid using NVARCHAR (MAX)

UserRoles table has a column name UserId which is nvarchar(128).

Screenshot of source code snippet displaying UserRoles table has a column name UserId which is nvarchar(128).

But in a stored procedure we have declared a parameter @UserId which is nvarchar(max) that is dealing with UserId column of UserRoles table which is incorrect.

Screenshot of source code snippet displaying stored procedure we have declared a parameter @UserId which is nvarchar(max) that is dealing with UserId column of UserRoles table which is incorrect.

      4. Try to avoid using Cursor

Cursors have poor performance.

Cursor and stop sign over source code snippet.

How you can avoid cursor?

  • The cursor can be written as a normal query in case of scenarios when the task needs to be performed repeatedly.
  • Rewrite cursors as a derived query.
  • Use temporary tables instead of cursors.
  • Use table variable instead of cursors.
  • Rewrite them as complex SQL Statements.

     5. Try to avoid writing DECLARE many times, try to write it one time

Screenshot of source code snippet displaying not to use DECLARE many times and rather try to write it one time.

   6. Try to avoid writing Scalar-valued function inside SELECT

If the Scalar-valued function is small then it is okay but if the function is big then it is better to separate it from join. It works a sub query inside SELECT which makes the query slow.

Screenshot of source code snippet displaying how to avoid writing Scalar-valued function inside SE.

    7. Use of DISTINCT

Do not use DISTINCT unless it is a business need. If we use DISTINCT in a big JOIN statement to retrieve unique value then at join query is defective. In this example we are using GROUP BY so there should not be any duplicate. Here we use DISTINCT also to remove duplicate. That means it’s a defective query.

Picture9

      8. Try not to use IN in WHERE Clause

IN makes the query slower, instead we can use join.

Screenshot of source code snippet displaying why to avoid using IN in WHERE Clause.

9. Use Non Clustered index on those columns which are used inside WHERE Statement frequently

10. Instead of HAVING use WHERE to define the filters

HAVING statements are executed after WHERE. A WHERE statement is more effective if the goal is to filter a query based on conditions.

Screenshot of source code snippet displaying of using WHERE instead of HAVING to define the filters.

11. Guidelines for GROUP By

  • Try not to use GROUP BY inside a big JOIN.
  • Try not to use GROUP BY when we are dealing with a big amount/rows of data.
  • Try not to use Group BY when we are dealing with a large number of column.
  • Try not to use Group By when we are not using any aggregate functions, Use DISTINCT instead.

12. Try not to use VIEWS inside a large JOIN query.

Views makes a join slower, so we can retrieve the query from view and make it precise and use Temp table to link it with JOIN query.

13. Use joins with INNER JOIN (not WHERE)

We can write this query,

Screenshot of source code snippet displaying INNER JOIN example.
Screenshot 5

14. Replace OR with CASE WHEN Statement

Picture10
Screenshot of source code snippet displaying why to Replace OR with CASE WHEN Statement.

15. Avoid writing large JOIN query. It will take so much time. Try to break it smaller join

Avoid large JOIN block unless it is extremely needed.

Screenshot of source code snippet displaying why to avoid writing large JOIN query.

16. LIMIT can simplify query results

Using a LIMIT statement prevents taxing the production database with a large query, only to find out the query needs editing or refinement.

Screenshot of source code snippet displaying LIMIT example.

17. Stop using sub query inside SELECT statement

It will makes the query execution time significantly low.

Screenshot of source code snippet displaying why to stop using sub query inside SELECT statement.

We can we do?

  1. Separate it from the main query and using TEMP table to link it with main query.
  2. We can keep the data in the main table for example.

We can make a new column ChallanNo in Order table and we can keep ChallanNo when a challan entered against an order then we can directly use ChallanNo column instead of subquery.

18. At the beginning make a block of precise data

Let’s consider the example. We are interested to retrieve the shop inside this rectangle.

Image of a map, showing different locations and their tags.

Now the shop locations are kept in Location table. So, if we directly use Location table inside JOIN statement then it will make the query slower. Because the location table may contain 100,000 rows of data.

Screenshot of source code snippet displaying why to make a block of precise data at the beginning.

We can make a precise block of Location data first inside that rectangle (maximum 500 data).

Screenshot of source code snippet displaying precise block of Location data.
Screenshot of source code snippet displaying precise block of Location data.

19. Do not use a big table inside a JOIN query. That will make the query slower

Screenshot of source code snippet displaying why not to use a big table inside a JOIN query.
Screenshot of source code snippet displaying why not to use a big table inside a JOIN query.

20. Check execution plan which block is taking time

Execution plan really helpful. We need to learn how to use it more efficiently.

Screenshot of source code snippet displaying how to check execution plan.

Check out blog posts from our developers on Mechanism for Error Handling in MS SQL Server Transact-SQL and Fixing Update Statement Errors Caused by Foreign Key Constraints in SQL.

Feel free to leave any comments for clarification, changes, or improvements. Also, you can contact with iXora Solution expert teams for any consultation or inquiry from here.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *