|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 10:54 PM
Points: 2,467,
Visits: 2,059
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 11:51 AM
Points: 5,232,
Visits: 7,023
|
|
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
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, April 23, 2013 7:06 AM
Points: 10,
Visits: 26
|
|
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!).
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: 2 days ago @ 7:28 AM
Points: 1,
Visits: 14
|
|
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
|
|
|
|