Avatar of dnadeveloper
dnadeveloperFlag for United States of America

asked on 

BETWEEN in a SELECT is slow.

I’m using SQL Server 2000 and a BETWEEN in the WHERE clause.
My basic query is:
SELECT * FROM mytable WHERE 3507184416 between RangeLow AND RangeHigh

There is a clustered index on RangeLow and a nonclustered index on RangeHigh.
Both fields are BigInt and there are about 7 million records in the table.
A query like the following will complete immediately. .
SELECT MAX(RangeLow) from  mytable where RangeLow <= 3507184416
If I use between it takes about 4.5 minutes.
Anyone know what I should be doing for this to work properly?

TIA,
Chris
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
dnadeveloper
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

what is the execution plan (can you upload the graph?)

what about this?
SELECT * FROM mytable
WHERE RangeLow <= 3507184416
     AND RangeHigh >= 3507184416

Avatar of dnadeveloper
dnadeveloper
Flag of United States of America image

ASKER

It is doing a clustered index seek. IO cost 310 CPU Cost 4.16  which accounts for 100% of the cost.
When I said "my basic query" above, that's really the whole thing.

I have tried using <= and >= but it performs the same.
It seems that the non clustered index is not getting used.
Avatar of Nightman
Nightman
Flag of Australia image

You also may want to look at index fragmentation - large range queries will result in an index scan, and if your indexes are fragmented this will hurt performance.

Check the indexes on your table with DBCC SHOWCONTIG ('tablename').
Avatar of Nightman
Nightman
Flag of Australia image

What data do you want from the table? Try including only the columns you need instead of SELECT * - if these are all included in the non-clustered you will get better performance.

Also, how many rows do you expect to be returned by the query?
Avatar of dnadeveloper
dnadeveloper
Flag of United States of America image

ASKER

Here is the execution plan:
http://www.dotnetassets.com/between.jpg
Avatar of dnadeveloper
dnadeveloper
Flag of United States of America image

ASKER

I only expect 1 row back. The table was just created and indexed so I do not suspect fragmentation. This seems like SQL server does not know how to use the indexes.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of dnadeveloper
dnadeveloper
Flag of United States of America image

ASKER

Yep,
That one screams and I can also add   rangeHigh >= 3507184416 and ..
to the where clause and it still goes real fast.
Thank you very much. It now completes immediately.

Chris
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo