Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

s
Pritom Nandy
More...

Resources » SQL Server

SQL SELECT Statement Performance Tuning Tips


Posted Date:     Category: SQL Server    
Author: Member Level: Gold    Points: 7


In this Article I am going to Explain about SQL SELECT Statement Performance Tuning Tips. Performance Tuning is important for Increase the Performance of the application. Here You will learn About SQL Server and T-SQL Performance tuning techniques. Learn SQL SELECT Statement Performance Tuning Tips



 


About SQL SELECT Statement Performance Tuning Tips


In this Article I am going to Explain about SQL SELECT Statement Performance Tuning Tips. Here You will learn About SQL Server and T-SQL Performance tuning techniques.


The DISTINCT clause creates a lot of extra work for SQL Server, and reduces the physical resources that other SQL statements have at their disposal. Because of this, only use the DISTINCT clause only if it is necessary.


Check to see if the data being returned has any chances of returning duplicates. If not, remove the DISTINCT clause.

Usually it is better to rewrite the query's FROM and WHERE clauses to use a sub query to filter the data correctly so that you only get back what you want.

Also, when using the UNION statement, keep in mind that, by default, it performs the equivalent of a SELECT DISTINCT on the final result set.

In other words, UNION takes the results of two like Recordsets, combines them, and then performs a SELECT DISTINCT in order to eliminate any duplicate rows.

This process occurs even if there are no duplicate records in the final Recordset.

So, if you know that there will never be any duplicate rows, or if there are, and this presents no problem to your application, then you should use the UNION ALL statement instead of the UNION statement.

The advantage of UNION ALL is that is does not perform the SELECT DISTINCT function, which saves a lot of unnecessary SQL Server resources from being used.

Following are 2 simple & powerful SQL tips which will help in writing better performing queries
1. If you need to verify the existence of a record in a table, don't use SELECT COUNT (*) in your Transact-SQL code to identify it, which is very inefficient and wastes server resources. Instead, use the Transact-SQL IF EXISTS to determine if the record in question exists, which is much more efficient.
For example:
Here's how you might use COUNT(*):

IF (SELECT COUNT(*) FROM tablename WHERE columnname = 'ABC')

Here's a faster way, using IF EXISTS:

IF EXISTS (SELECT 1 FROM tablename WHERE columnname = 'ABC')

The reason IF EXISTS is faster than COUNT(*) is because the query can end immediately when the text is proven true, while COUNT(*) must count go through every record, whether there is only one, or thousands, before it can be found to be true.

2. EXISTS drives from out-to-in. i.e. For each row returned by the outer query, the database executes the join in the sub-query. INs drive from in-to-out. If the outer query is more selective than the inner one, use an EXISTs. If the inner query is more selective, use an IN. Accordingly, change this

SELECT ... FROM tablenameD D WHERE EXISTS (SELECT 1 FROM tablenameE E WHERE E.ID=D.ID)

to this …

SELECT ... FROM tablenameD D WHERE D.ID IN (SELECT E.ID FROM tablenameE E)

I think It will help you about SQL SELECT Statement Performance Tuning Tips. Thanks for reading my Article SQL SELECT Statement Performance Tuning Tips . if you have any query or you have any suggestion, let me know. I will appreciate you valuable feedback.

Thanks
S.Suresh





Did you like this resource? Share it with your friends and show your love!


Responses to "SQL SELECT Statement Performance Tuning Tips"
Author: SonyMadhu    12 Jul 2012Member Level: Gold   Points : 0
Hi suresh,


Thanks for sharing this information, it is useful for me.



Author: Suresh Sellathambi    13 Mar 2013Member Level: Gold   Points : 0
Hi Sony Madhu,

Thanks for your Appreciation.



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Count how many Sundays, Mondays, Tuesdays... in current month using MS SQL
    Previous Resource: Date and Time Functions of SQL Server 2008 R2
    Return to Resources
    Post New Resource
    Category: SQL Server


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    SQL Server Performance Tuning  .  Performance tuning Tips for SQL Server  .  Distinct Keywords  .  SQL SELECT Statement Performance Tuning  .  SQL Server Where Clause  .  
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.