SQL Server Query and Transact SQL Performance Tuning
In Applications where SQL Server is used as a back-end for data storage and manipulation purposes, there are various reasons for SQL Queries or Transact SQL Statements or Code running slow when the Application is executed in a Production Server Environment. Some of the reasons for slow running queries and updates are:
- Low network speed or slow network communication.
- Inadequate memory in the server computer or not enough memory available for SQL Server.
- Improper indexing in database.
- Lack of usage of database statistics.
- Lack of proper database partitioning.
To facilitate development of applications with reliable and faster performance, there are some basic SQL Server Tools and Techniques to enhance the Query and Transact SQL Performance.
Tools like Performance Monitor and SQL Server Profiler can be used to monitor the Performance of SQL Server and non-SQL related components. Some of the SQL Server related components and events that are monitored are Events related to SQL Server Service Broker Components, SQL Server service Components, Database Operation Events, Cursor Events, Transaction Events , Replication Events etc. Other non-SQL components include System Memory, Processor, Other Application and System Processes, System Security etc.
Few techniques to determine slow running queries and Transact SQL statement include using dynamic management views - sys.dm_exec_query_stats and sys.dm_exec_request to find queries that collectively consume a large number of resources and analyzing query performance by producing a showplan.
Techniques for improving query performance include prioritizing SQL Server over other operating system processes, Choosing and Creating the Right indexes and use query optimization techniques to improve individual query performance.
Indexing of Tables should be used very judiciously by choosing the most critical queries and determining the index columns based on their usage frequency. Also judicious usage is recommended considering that there are trade-offs involved with this technique in the sense that your SELECT queries might run faster but your INSERT, UPDATE and DELETE operations will slow down. Some useful ways of determining creation of indexes are:
- Create selective indexes on columns that are used in the WHERE clause of queries.
- Create multiple column indexes which are used in filter expressions and also make sure that the most selected column is left most in the indexed key.
- Indexing small tables will deteriorate performance.
Following individual query optimization techniques help to improve the overall performance of your queries as well as the application:
- Use 'WHERE' clause to limit the number of rows wherever possible.
- Use 'UNION ALL' instead of 'UNION' where no duplicate rows are returned in the result sets of the queries which are being merged. However if there are duplicate rows and they need to be eliminated then use 'UNION'.
- Avoid using 'DISTINCT' clause where duplicate rows are not returned as it leads to SQL Server using more resources thereby causing performance bottlenecks in the long run.
- Avoid using Column or Row constraints in Tables as they degrade Insert and Update query performance.
- Avoid using function or calculation on the column in the where clause.
Other techniques include using database-wide statistics options, partitioning tables, views and indexes when large volume of data is involved and updating statistics more frequently
For more detailed reference on SQL Performance Tuning Refer to SQL Tips or SQL Server MSDN forums

