Solved

SQL Server 2005 - "Select" cannot use optimum index

Posted on 2008-10-29
8
867 Views
Last Modified: 2008-11-04
I am using Microsoft SQL Server 2005.
There is a datetime column, sales_date, in the table sales.
I have created an index in the table: -
create index idx_sales on sales(sales_date)

When I issue the follow SQL: -
select * from sales where sales_date = '20081030'
The result prompt out immediately.  I figure out from the Execution Plan that it has used the correct index, idx_sales.

When I issue the follow SQL: -
select * from sales where sales_date between '20080801' and '20081030'
The result prompt out immediately, too.  The correct index, idx_sales is used.

However, when I issue the follow SQL: -
select * from sales where sales_date between '20080501' and '20081030'
(Any date range  more than two months)
The result come out very very slowly.  From the Execution Plan, the index of the primary key has been used.  (It means that "table scan" has been performed).

The table has been set as "IsAutoCreateStatistics" and "IsAutoUpdateStatistics".
So, why the correct index cannot be used?

Thx!

0
Comment
Question by:roland_lei
[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
8 Comments
 
LVL 18

Expert Comment

by:jmoss111
ID: 22838244
How is the index ordered? I've found  that ordering by date descending makes a bit of difference on large tables.

Are all the columns really necessary?
0
 

Author Comment

by:roland_lei
ID: 22838442
The order of the index is decending.

All other columns are necessary.  Do other columns affect the correct selecting of index?
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 22838457
You have two options:

1)  Force the index (using index hints)

2)  Try alternate forms of the query, such as:

select * from sales where sales_date >= '20080501' and sales_date <= '20081030'

There can be quirks in the query optimizer that incorrectly affects the query plan.  Try 2 first, but if it does not work, try 1.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22838468
please try to rebuild the index, or defrag the index, or update the stats on the index...
0
 

Author Comment

by:roland_lei
ID: 22838495
Rewriting as
select * from sales where sales_date >= '20080501' and sales_date <= '20081030
will have no change.

And I would not like to force the index because couple of programs should be amended.

I have dropped the index and recreate it and then update the statistics.  There is no change, too.
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 22841244
Sometimes the query analyzer simply fails to optimize correctly.  Do not assume that rewriting the query to use a different format will do nothing, as I have shown in my own queries that it can (but will not always, or even frequently) correct issues with bad query plans.

What is happening in this case is that the query analyzer is deciding that, with the number of records you are selecting, the end speed *should* be faster using a pk / clustered index (is the primary key clustered?) scan than it would be by using the index.

If you test by forcing the index, does the resulting query return more quickly than without in the poorly=performing case?  Or does it simply return the first rows faster, thus seeming more responsive, but take longer or more resources to complete the full query?
0
 

Author Comment

by:roland_lei
ID: 22850550
I have done two tests with different date ranges.

1st (shorter date range)
The time when running the full query with forcing index is 19 seconds.
The time when running the full query without forcing index is 39 seconds.

2nd (longer date range)
The time when running the full query with forcing index is 25 seconds.
The time when running the full query without forcing index is 44 seconds.

It means that the index is useful.  However, the query optimizer cannot use the appropriate index.
0
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 500 total points
ID: 22869607
Yes, this would be a failure inside the optimizer code in SQL Server itself.   The statistics and assumptions used by the optimizer apparently do not handle this particular table and index structure as a completely optimized process.

The reasons can vary from excessive clustered index fragmentation in the table itself (i.e. if the file was completely defragmented, the clustered index would actually be faster, but since it is not, the end result is slower response time) to unusual data distribution (i.e. the optimizer looks at the statistics and decides that the data is not sufficiently selective for the period covered - a problem that may fix itself as more data is added to the table) to beyond threshold resource usage (i.e. the optimizer compares the resource usage of the queries - even though query 1 is faster, it may use more resources, and so the optimizer uses option 2) to simply a case of the assumptions and rules built into the optimizer code messing up on this specific example.

The index hints are available to handle the cases where the optimizer chooses a non-optimal method.  I think that's what you will need to do unless patching to a new SP or hotfix corrects the issue - and there is no guarantee that it will.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

622 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