Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this query faster with multiple selects rather than using between?

I have a table in Sql Server 2008 Express which contains 18 million records. The structure looks something like this (simplified):

Id, GroupId, Value, Created

Id is the primary key with a clustered index
GroupId is a non-clustered index

In this case, every 10 rows get a new groupId meaning that records 1-10 have GroupId 1, records 11-20 have GroupId 2 and so on.

Test 1: This query takes 23 seconds to run and returns 99 records:

DECLARE @Start INT
SET @Start = 1050
select * from FieldValues where GroupId between @Start and @Start + 10

Test 2: This query takes 0 seconds to run and returns 99 records:

DECLARE @Start INT
SET @Start = 1050
select * from FieldValues where GroupId = @Start union
select * from FieldValues where GroupId = @Start + 1 union
select * from FieldValues where GroupId = @Start + 2 union
select * from FieldValues where GroupId = @Start + 3 union
select * from FieldValues where GroupId = @Start + 4 union
select * from FieldValues where GroupId = @Start + 5 union
select * from FieldValues where GroupId = @Start + 6 union
select * from FieldValues where GroupId = @Start + 7 union
select * from FieldValues where GroupId = @Start + 8 union
select * from FieldValues where GroupId = @Start + 9 union
select * from FieldValues where GroupId = @Start + 10


Note: Since results can get cached i always scramble the @Start variable between each test to get non-cached time estimations

Why does these multiple selects (which looks like some beginner have throught up) go so much faster than the more elegant one in test 1?

like image 834
CodeSpeaker Avatar asked Jan 29 '26 01:01

CodeSpeaker


1 Answers

Try using the "Show actual execution plan" in the query analyser and you will see that the second query is probably achieving the results by performing an index seek, whereas the former (slower) is not able to do this because it doesn't know that the records are sequential because the index it is using is non-clustered.

like image 64
Bernhard Hofmann Avatar answered Jan 31 '26 13:01

Bernhard Hofmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!