Log in  ::  Register  ::  Not logged in

 Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 A Faster BETWEEN Dates Rate Topic Display Mode Topic Options
Author
 Message
 Posted Friday, November 05, 2010 2:09 AM
 SSCrazy Group: General Forum Members Last Login: Yesterday @ 7:44 PM Points: 2,706, Visits: 2,300
 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 Group: General Forum Members Last Login: Yesterday @ 2:09 PM Points: 5,600, Visits: 7,799
 sql.monkey (11/4/2010)I have a table with an unindexed date column in a table of billions of rowsI collect the lowest and highest primary keys between those dates into variables I set two variablesdeclare @min int declare @max intselect @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 @maxworks for meYou'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 usingSELECT @min = min(primarykey), @max=max(primarykey)FROM tableWHERE 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 useSELECT primarykey, datecol, x,y,zFROM tableWHERE 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 MVPVisit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1016412
 Posted Tuesday, November 09, 2010 8:35 AM
 Grasshopper Group: General Forum Members Last Login: Wednesday, August 28, 2013 9:54 AM Points: 11, Visits: 32
 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 Group: General Forum Members Last Login: Monday, October 28, 2013 4:39 AM Points: 1, Visits: 17
 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 angleChris
Post #1116070

 Permissions