Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Clustered Index Scan Vs Clustered Index Seek

Posted on 2004-04-20
4
Medium Priority
?
1,946 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 1400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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