Solved

Clustered Index Scan Vs Clustered Index Seek

Posted on 2004-04-20
4
1,935 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

630 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