Solved

Clustered Index Scan Vs Clustered Index Seek

Posted on 2004-04-20
4
1,930 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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server 2012 r2 and SQL 2014 6 34
How to use Recordset, returned from a SPROC as the recourdsource of a form. 24 52
VM SQL server license. 1 67
TDE for SQL Web Edition 1 42
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

710 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