Solved

Clustered Index Scan Vs Clustered Index Seek

Posted on 2004-04-20
4
1,917 Views
Last Modified: 2007-12-19
Please view the query/UDF posted in the following related question.

I'd like to know how I can design my indices so that a clustered index scan can be avoided.

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20960243.html

Thanks,

Billy.
0
Comment
Question by:billy21
  • 2
  • 2
4 Comments
 
LVL 6

Author Comment

by:billy21
ID: 10867640
I've also posted the related table structure.
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10867992
Create Function dbo.SOSCurrentVary(@StartDate DateTime)
Returns Table
AS
Return
     Select Va.SOSID,
     Max(Case When Va.VDate = @StartDate Then Units End) CurUnits1,
     Max(Case When Va.VDate = @StartDate+1 Then Units End) CurUnits2,
     Max(Case When Va.VDate = @StartDate+2 Then Units End)  CurUnits3,
     Max(Case When Va.VDate = @StartDate+3 Then Units End)  CurUnits4,
     Max(Case When Va.VDate = @StartDate+4 Then Units End)  CurUnits5,
     Max(Case When Va.VDate = @StartDate+5 Then Units End)  CurUnits6,
     Max(Case When Va.VDate = @StartDate+6 Then Units End)  CurUnits7
     From SOSVary Va
     Group By Va.SOSID
)


In the query above, there's no restriction on fields that could make the query more selective and force index seek rather than index scan.
The optimiser will rely on statisitcs to decide what's better.

An index seek will be performed if the query is selective.
The query above obviously needs to make a scan because there's no condition on the SOSID

Index seek will result in a performance boost if, say, 95% of the rows can be excluded by your where clause

You could maybe make it more selective using an additional where clause

Check execution plan for
    Select Va.SOSID,
     Max(Case When Va.VDate = @StartDate Then Units End) CurUnits1,
     Max(Case When Va.VDate = @StartDate+1 Then Units End) CurUnits2,
     Max(Case When Va.VDate = @StartDate+2 Then Units End)  CurUnits3,
     Max(Case When Va.VDate = @StartDate+3 Then Units End)  CurUnits4,
     Max(Case When Va.VDate = @StartDate+4 Then Units End)  CurUnits5,
     Max(Case When Va.VDate = @StartDate+5 Then Units End)  CurUnits6,
     Max(Case When Va.VDate = @StartDate+6 Then Units End)  CurUnits7
     From SOSVary Va
     Where Va.VDate between  @StartDate and @StartDate+6
     Group By Va.SOSID

0
 
LVL 6

Author Comment

by:billy21
ID: 10868191
It still uses a clustered index scan but based on what you said, it sounds like it's necessary to use the scan.
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 350 total points
ID: 10868253
I think so, since there's a "group by" and no filter.
I guess the same query with a filter on a particular SOSID should use an index seek.
(again, if and only if, based on the statistics, the optimizer decides that the query is selective enough to justify seek vs scan)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now