Previous | Table of Contents | Next |
Many students ask which one is bettera clustered or nonclustered index? For many queries, a clustered index is more efficient than a nonclustered index on the same column(s). But only one clustered index can be created per table, so most tables have a combination of clustered and nonclustered indexes.
The following list helps to answer the question. Rather than ask Which is better?, it might be better to ask What are their relevant strengths?
Every time you execute a SQL batch, SQL Server runs through a four-step process:
Now that weve looked at how SQL Server actually uses the different types of indexes when executing the query, the next step is to understand how SQL Server decides which indexes to use on a specific query. The process of choosing an access method for a query is called optimization.
Optimization is the process of selecting an access path for resolving a query.
Note: This is by far the most difficult aspect of SQL Server for experienced programmers to grasp. SQL Server will decide when and where to use indexes to make queries more efficient. As we step through the decision process, you will see how that process is affected by current characteristics of the data, so it is far more flexible and efficient over time than you could be by deciding to write a certain program in a certain way.Nevertheless, this is the part thats hard to get used to. The server is seldom wrong about the most efficient path to retrieve the data, but the loss of control that comes with the efficiency leaves programmers struggling for something concrete.
SQL Server uses a cost-based optimizer, which means that the server focuses on the cost (in terms of physical resourcesprincipally disk and memory reads) of performing a query using a certain approach.
The optimization process itself consists of several steps:
Well walk through this process ourselves for a sample query. Heres the query:
select count(*) count, sum(qty) total sold from sales where stor_id between F and Gz and title_id like BI2%
SQL Server first tries to find elements of a query that will allow the server to use an index search arguments, or SARGs.
A SARG is a portion of a WHERE clause that is in the form
column-name comparison-operator constant-expression
as in
title_id like BI2%
or
qty > 500
Only SARGs (or so-called improvised SARGs) can take advantage of indexes. So the first step in improving your queries is to make certain that the server is able to recognize SARGs in your WHERE clauses.
Do......Dont
|
In the case of our example, there are two SARGs: the BETWEEN clause for stor_id and the LIKE test for title_id.
At this stage, SQL Server looks for indexes that might be useful for the query. Each column referenced by a SARG might have one or two relevant indexes (or more). The server also identifies composite indexes that might be useful.
Note: The optimizer only uses an index if one of the search arguments is the FIRST column in the index. Many programmers expect that an index will be effective for a query because a column is included in an index, but its not true unless that column is the FIRST column.
In this case, SQL Server finds two candidate indexes, one for each SARG.
Previous | Table of Contents | Next |