I have a huge table, having a much smaller number (by orders of magnitude) of distinct values on some column x.
I need to do a query like SELECT D
No. But there are some workarounds (excluding normalization):
Once the index is in place, then its possible to implement in SQL what the optimizer could be doing automatically:
https://stackoverflow.com/a/29286754/538763 (multiple workarounds cited)
Other answers say you can normalize which would solve your issue but even once its normalized SQL Server still likes to perform a scan to find the max() within group(s). Workarounds:
https://dba.stackexchange.com/questions/48848/efficiently-query-max-over-multiple-ranges?rq=1