In this SQL tutorial we want to give you an overview of how to improve query performance by looking at the following areas:
The most frequent access to a database is to retrieve data from it using SELECT statements simple or complex as the case may be. As a result, measuring and improving query performance can enhance SQL Server performance greatly.
You can measure the performance of queries using three main metrics:
Each of these metrics will now be explained.
Query cost assesses the CPU and the I/O resources that were used during the execution of a query.To view the query cost, in the SQL Server Management Studio:

A low query cost is an indicator of good query performance. However, query cost does not account for resource contention or the time spent in dealing with locks.
Also, it does not include the cost resulting from user-defined functions and Common Language Runtime (CLR) routines.
Thus, this query cost is often referred to as estimated query cost.
SQL Server reads data from storage in pages, where each page is 8KB. Page reads are a set of statistics that indicate how many pages were read from storage. To view page read metrics, before you execute the query, execute the command:
SET STATISTICS IO ON
Now when you run a query, the Messages tab of the Results window displays the page reads details. All logical reads must be totaled to get the page reads details as shown in the screen.

Note: Logical reads are pages read from the cache during query execution, while physical reads are pages read from the disk. It is possible that a page is read multiple times from the cache and so logical reads may be considerably higher than physical reads. You can lower logical reads by using well-planned indexes.
Query execution time measures the total execution time of a query in milliseconds. However, it also includes time consumed by resource contention and negotiating locks.
To view execution time, before you execute the query, execute the command:
SET STATISTICS IO ON

Among the various ways to tune the performance of a query are:
You can use one or more of these approaches to improve the performance.
Before we can optimize a query, let us take a look at how a query is executed. The typical order in which clauses in an SELECT statement are executed is shown in the table below.
Note that the execution order varies when a UNION operator is used. In addition, the Query Optimizer may change the execution order to improve performance.
| Execution Order (Including the UNION clause) |
Execution Order |
| FROM, JOIN, APPLY, and ON | FROM, JOIN, APPLY, and ON |
| WHERE | WHERE |
| GROUP BY and aggregate functions(included in the query) | GROUP BY and aggregate functions(included in the query) |
| HAVING | HAVING |
| TOP | SELECT |
| UNION and SELECT | ORDER BY |
| ORDER BY | TOP |
| FOR XML | FOR XML |
To select queries for performance, use the SQL Server Profiler to monitor the SQL:BatchCompleted and RPC:Completed events with a focus on the following columns:
Any increase in the column values above the desired level implies that performance of the queries requires tuning.
You can also examine the Graphical Execution Plan as a means of identifying bottlenecks in query execution. The table below lists the visual components and how they can be used to optimize queries.
| Components | Indications |
| Thick arrow | The thicker the arrow, greater is the number of rows being transferred from one operation to another. |
| Hash operation | Hash operations on GROUP BY and JOIN operations indicate lack of appropriate indexes. |
| Sort operation | A high cost for a sort operation may indicate the need for an index. |
| Table or clustered index scan | Indicate lack of appropriate indexes on the given tables. |
To view the graphical execution plan, in the SQL Server Management Studio:
A WHERE clause helps you to restrict the number of rows returned by a query. However, the manner in which the WHERE condition is specified can impact the performance of the query. If the WHERE condition is written such that it uses a function that takes an indexed column as the input, then the index is ignored and the entire table is scanned. This results in performance degradation.
For example, the following results in a table scan because the column OrderDate is used in a function:
SELECT CustomerID, EmployeeID FROM Orders WHERE DATEDIFF(m, OrderDate, GetDate())>3
If the function is rewritten as shown below, then the query seeks the required value using an index and this improves performance:
SELECT CustomerID, EmployeeID FROM Orders WHERE OrderDate < DATEADD(m, -3, GetDate())
The filter criteria in the second query is said to use a Searchable Argument or SARG because the query optimizer can use an index seek operation during execution.
Joins in queries increase the cost of the query and thereby lowers its performance. Hence, it is best to minimize the number of join clauses to ensure optimized query performance.Outer joins incur more cost than inner joins as an outer join also works with the unmatched rows of the two tables. Queries with only inner joins and the WHERE clause have similar execution and therefore have similar performance.
There are many ways in which a query can be written, we hope this brief SQL tutorial has given you some more ideas in improving query performance.
Copyright © 2011 ViSteps Pty Ltd. All rights reserved.
SQLSteps.com is a business wholly owned and operated by ViSteps Pty Ltd.