Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase «««23456

A Faster BETWEEN Dates Expand / Collapse
Posted Friday, November 5, 2010 2:09 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:53 PM
Points: 3,463, Visits: 2,984
Michael Ebaya (11/3/2010)
happycat59 (11/3/2010)
Not only is the original article of interest (and it is great to have someone prepared to write about their findings...thanks Terry)
Does no one actually care the entire article is wrong, top to bottom?

The reason for my interest is not just the original post. The discussion that it has generated really does show how much interest there is in this topic. Yes, there have been concerns expressed about whether the OP's original solution is equivalent to the original code. The fact that there are so many replies that have corrected the error or, at least, pointed it out means that I am not concerned. It has made people think and that is more important than anything else

Post #1016399
Posted Friday, November 5, 2010 2:38 AM



Group: General Forum Members
Last Login: Thursday, April 28, 2016 7:50 AM
Points: 7,602, Visits: 10,414
sql.monkey (11/4/2010)
I have a table with an unindexed date column in a table of billions of rows
I collect the lowest and highest primary keys between those dates into variables

I set two variables

declare @min int
declare @max int

select @min = min(primarykey) where datecol => 'begindate'
select @max= max(primarykey) where datecol <= 'enddate'

select primarykey, datecol, x,y,z from table where primarykey between @min and @max

works for me

You'll get a syntax error - no FROM clause in the two queries that set @min and @max.

After fixing that, if the datecol column is indeed unindexed, you get two complete table scans for setting the @min and @max variables. You can reduce that to one scan by using
SELECT @min = min(primarykey), @max=max(primarykey)
FROM table
WHERE datecol BETWEEN @begindate AND @enddate;

But it's still a scan. The same scan you would get if you throws away all the unnecessary logic and use
SELECT primarykey, datecol, x,y,z
FROM table
WHERE datecol BETWEEN @begindate AND @enddate;

If you check the execution plan for your query, you will probably find that is uses an index on the datecol column that you had forgotten existed.

And the objection posted by GPO is valid as well - this (useless) technique only gives the correct results if ascending key order and ascending datecol order match up completely. Which is probably only the case if one column is an IDENTITY and the other has a DEFAULT(CURRENT_TIMESTAMP) and is never ever manually changed.

Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog:
Post #1016412
Posted Tuesday, November 9, 2010 8:35 AM


Group: General Forum Members
Last Login: Tuesday, April 26, 2016 6:44 AM
Points: 17, Visits: 57
I just tried this technique on a moderate table size - BETWEEN was around twice as fast. I have a feeling that the improvement of BETWEEN would scale as the data does (indexing and statistics come in to play as well). I also concur with Hugo's statements (imagine that Hugo, we agree!).

Post #1017983
Posted Friday, May 27, 2011 2:58 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 14, 2015 10:27 AM
Points: 1, Visits: 21
Wouldn't you be better off using temporary tables rather than table variables, i was under the impression I should only use table variables for very small (500 records) amounts of data?

Sorry I have read more replies and see that table variables were being used as examples and other replies have covered the table variables angle

Post #1116070
« Prev Topic | Next Topic »

Add to briefcase «««23456

Permissions Expand / Collapse