I have a large table I have been playing with the query analyzer and looking for the best way to do this.
The table is like this:
name rows reserved data index_size unused
table_name 110980132 7802944 KB 6119784 KB 1679320 KB 3840 KB
And has these columns:
ID int, time_stamp datetime, value1 float, value2 float, value3 float....
These time_stamps are dates with times. I need to find an easy way, without storing anything, to be able to get just the date portions for the table. Eventually, I may need to know just the day + hour part (and not the whole time portion). At the moment, I just need to know what the last 30 days we had data for are (sometimes days are missing at this point, this question/query will ultimately not just be looking for the last x days, but all the days, or whatever).
What is the best way to do this considering performance and time? I’ve played with group by, distinct, top x, rank(), temp tables, views… some things are better than others but nothing I am doing seems to be great.
Ideas? Thanks!
If you’re open to using T-SQL batches, instead of a single query, then you can make use of indexes like this:
All this requires of you is a good index on
time_stamp, and it will perform exactly 30 seeks (or less) on that index. Very surgical and quick. I threw it up as a concept, so obviously the 2 scalar subqueries in there can be easily optimised.