Can I optimize a SELECT DISTINCT x FROM hugeTable query by creating an index on column x?

后端 未结 8 846
闹比i
闹比i 2021-01-01 11:51

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

8条回答
  •  情深已故
    2021-01-01 12:00

    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

提交回复
热议问题