Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


select data between 2 periods


select data between 2 periods

Author
Message
leonos
leonos
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 47
I have a table with a big amount of data.
In my application I need to fetch data from this table entered between two dates (let's say 1 month).

There are three ways of doing this:
1. SELECT info FROM table WHERE tabledatetime >= myStartdate AND tabledatetime <= myEnddate
2. SELECT info FROM table WHERE tabledatetime BETWEEN myStartdate AND myEnddate
3. SELECT info FROM table WHERE year(tabledatetime)=myYear AND month(tabledatetime)=myMonth

1 is the slowest, that's not hard.

But what about the other 2, which one will give me the best performance.

greetz
Leon
5409045121009
5409045121009
SSC-Enthusiastic
SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)

Group: General Forum Members
Points: 152 Visits: 666
Try a covering index on the date column and the info column your are looking for. In other words a non cluster non unique index on the two columns and switch displaying of the querying plan on when executing your query. Alternatively a partitioned view/table can help.



Michael Earl-395764
Michael Earl-395764
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2707 Visits: 23078
Actually, 3 is the slowest once you have indexes on the table. Since you are using a function against your table column, SQL cannot use an index and it must resolve the function on every row in your table.
I would expect 1 and 2 to generate the same execution plan and perform exactly the same, but the confirm you need to examine the execution plans.
Adam Haines
Adam Haines
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2324 Visits: 3135
I agree with Michael here, you definitely do not want to use option 3. Since this table is big and seems to be a heavy hitter, I would make sure you have the proper indices to create a covering index.

Options 1 and 2 should evalute the same, so I would choose option 2 because it has an easier syntax and is easier to read/type BigGrin.



My blog: http://jahaines.blogspot.com
Chandra Sekhara Vyas Dhara
Chandra Sekhara Vyas Dhara
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3963 Visits: 1149
If you have index on myStartdate then between is faster.



GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47241 Visits: 44377
With an appropriate index*, the first 2 can do an index seek to get the data. The third, since it has functions applied to the columns, will do at best an index scan.

The first 2 are functionally identical and, if you check the exec plan, you'll notice that SQL converts between into the <=, >= construct.

If there are no indexes on tabledatetime, all three will likely be about the same

* Appropriate index should be a covering one, tabledatetime as the leading column and the columns in the select either as other index key columns, or as include columns


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5957 Visits: 8312
1) Be careful adding a bunch of columns to an index just to make it covering. There is maintenance cost and disk storage cost to this.

2) An index on the date won't be helpful if a months worth of data is more than ~8-10% of the table, unless said index is covering.

3) If your predominant access of this table is by date range, consider making the clustered index the date field used.

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search