C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Databases »

TSQL Optimization Tips


Posted Date: 23 Mar 2005    Resource Type: Articles    Category: Databases
Author: Rakesh Chander SharmaMember Level: Silver    
Rating: 1 out of 5Points: 10




Use views and stored procedures instead of heavy queries.


It reduces network traffic, coz client will send to server only stored procedure or view name (in certain cases heavy-duty queries might degrade performance upto 70%) instead of large heavy-duty queries text. This also facilitates permission management as you can restrict user access to table columns .

Use constraints instead of triggers


Constraints r much more efficient than triggers and can boost performance. So use constraints instead of triggers, whenever possible.

Use table variables instead of temporary tables.


Table variables require less locking and logging resources than temporary tables, so table variables should be used whenever possible (available in SQL Server 2000 only).

Use UNION ALL statement instead of UNION, whenever possible.


The UNION ALL statement is much faster than UNION, because UNION ALL statement does not look for duplicate rows, and UNION statement does look for duplicate rows, whether or not they exist.

Try to avoid using the DISTINCT clause, whenever possible.


Because using the DISTINCT clause will result in some performance degradation, you should use this clause only when it is necessary.

Try to avoid using SQL Server cursors, whenever possible.


SQL Server cursors can result in some performance degradation in comparison with select statements. Try to use correlated sub-query or derived tables, if you need to perform row-by-row operations.

Try to avoid the HAVING clause, whenever possible.


The HAVING clause is used to restrict the result set returned by the GROUP BY clause. When you use GROUP BY with the HAVING clause, the GROUP BY clause divides the rows into sets of grouped rows and aggregates their values, and then the HAVING clause eliminates undesired aggregated groups. In many cases, you can write your select statement so, that it will contain only WHERE and GROUP BY clauses without HAVING clause. This can improve the performance of your query.

If you need to return the total table's row count, you can use alternative way instead of SELECT COUNT(*) statement.
There r 2 ways to do this
SELECT COUNT(*) statement makes a full table scan to return the total table's row count, it can take very many time for the large table. There is another way to determine the total row count in a table. You can use sysindexes system table, in this case. There is ROWS column in the sysindexes table. This column contains the total row count for each table in your database.
So, you can also use the following select statement instead of

SELECT COUNT(*)
:
SELECT rows FROM sysindexes WHERE id = OBJECT_ID('table_name')

So, you can improve the speed of such queries in several times.
Use SET NOCOUNT ON statement into your stored procedures to stop the message indicating the number of rows affected by a T-SQL statement.

This can reduce network traffic, because your client will not receive the message indicating the number of rows affected by a T-SQL statement.

Use the WHERE clause.


Results in good performance benefits, because SQL Server will return to client only particular rows, not all rows from the table(s). This can reduce network traffic and boost the overall performance of the query.

Use the select statements with TOP keyword or the SET ROWCOUNT statement, if you need to return only the first n rows.


Can improve performance of your queries coz smaller result set will be returned. This can also reduce the traffic between the server and the clients.

Try to return only the particular columns from the table, not all table's columns.


Gives u good performance benefits, because SQL Server will return to client only particular columns, not all table's columns. This can reduce network traffic and boost the overall performance of the query.

Hope u find these tips useful.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: CREATING TABLES IN TO THE DATABASE
Previous Resource: SQL Server 2005 and CLR Integration
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use