Solved

SQL Server 2005 - "Select" cannot use optimum index

Posted on 2008-10-29
8
861 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
8 Comments
 
LVL 18

Expert Comment

by:jmoss111
Comment Utility
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
Comment Utility
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:bhess1
Comment Utility
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
please try to rebuild the index, or defrag the index, or update the stats on the index...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:roland_lei
Comment Utility
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:bhess1
Comment Utility
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
Comment Utility
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:
bhess1 earned 500 total points
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

7 Experts available now in Live!

Get 1:1 Help Now