1
2
|
SELECT * FROM sales.storecontact
SELECT * FROM sales.storecontact WHERE customerid=322
|
Clustered Index scan This is nothing but tables scan in a table
which has Clustered index. Since the clustered index leaf page contains
the data itself, performing a clustered index scan will scan all the
entire records which will affect performance. But as I mentioned earlier
if the records are fewer it wouldnt affect much.
For the below query the optimizer does an Clustered index scan to
retrieve the records,
1
|
SELECT * FROM sales.storecontact WHERE contacttypeid=15
|
Clustered index seek A seek will retrieve only selective
records from a table when compared to scan which will traverse all the
records in a table. If the seek operation is done using a clustered
index its a clustered index seek. Basically, any seek operation will
vertically traverse through the B-Tree and fetch the records.
Consider the below query for which I created a clustered index on
Customerid field. The optimizer uses the clustered index and performs a
clustered index seek.
1
|
SELECT * FROM sales.storecontact WHERE customerid=322
|
The clustered index seek will traverse through the records where the
customerid=322 and fetch the output. When compared to table scan which
will traverse through all the records, an index seek is very helpful in
reading the number of records quickly and is good for performance.
Index scan Since a scan touches every row in the table whether
or not it qualifies, the cost is proportional to the total number of
rows in the table. An index scan is nothing but a scan on the
nonclustered index. When index scan happens, all the rows in the leaf
level are scanned.
Index seek An index seek uses the nonclustered index to seek
the records in a table and is considered better for performance if there
is high selectivity.
For the below query the optimizer does an index seek using the NC index
on contacted field. Since the NC covers only the contactid it will not
be able to fetch all the records with an index seek alone. So its uses
seek to fetch the records which have contactid=322 and then does a key
lookup using the clustered index key to fetch the other fields records.
1
|
SELECT * FROM sales.storecontact WHERE contactid=322
|
The key lookup is an expensive operation if there are numerous records.
Since key lookup increases as the IO we might have to avoid it in some
cases. Index with included columns can help to overcome this situation
and cover the entire query and in turn
No comments:
Post a Comment