Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««23456

A Faster BETWEEN Dates Expand / Collapse
Author
Message
Posted Friday, November 05, 2010 2:09 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 9:21 PM
Points: 2,842, Visits: 2,423
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 05, 2010 2:38 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:24 AM
Points: 5,794, Visits: 8,009
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: http://sqlblog.com/blogs/hugo_kornelis
Post #1016412
Posted Tuesday, November 09, 2010 8:35 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 11:42 AM
Points: 12, Visits: 34
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, February 04, 2014 5:49 AM
Points: 1, Visits: 18
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

Chris
Post #1116070
« Prev Topic | Next Topic »

Add to briefcase «««23456

Permissions Expand / Collapse