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
As per your description of the execution plan, I would believe it's the best possible execution.
The Index Scan reads the entire index as stored (not in index order), the HASH MATCH does the distinct.
There might be other ways around your problem. In SQL Server, Indexed Views come to my mind. However, that might give you a big hit for write's on that table.