This question is largely due to curiosity, as I have a working request (this will take a little longer than I would like).
I have a table with 4 million rows. The only index in this table is the BigInt ID with auto-increment. The query searches for individual values ββin one of the columns, but returns only one day. Unfortunately, the ReportDate column that is being evaluated is not of type DateTime or even BigInt, but it has a char (8) value in the format YYYYMMDD. So the request is a bit slow.
SELECT Category FROM Reports where ReportDate = CONVERT(VARCHAR(8), GETDATE(), 112) GROUP BY Category
Note that converting the date in the above description simply converts it to the YYYYMMDD format for comparison.
I was wondering if there is a way to optimize this query based on the fact that I know that the only data that interests me is at the bottom of the table. I was thinking of some sort of recursive SELECT function that gradually increased the temporary table that could be used for the final query.
For example, in psuedo-sql:
N = 128 TemporaryTable = SELECT TOP {N} * FROM Reports ORDER BY ID DESC if(TemporaryTable does not contain ReportDate < Today) N = N**2 Repeat Select SELECT Category FROM TemproaryTable where ReportDate = CONVERT(VARCHAR(8), GETDATE(), 112) GROUP BY Category
It makes sense? Is something like this possible?
This is on MS SQL Server 2008.
source share