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 DISTINCT x FROM hugeTable
, and I want to do this relatively fast.
I did something like CREATE INDEX hugeTable_by_x ON hugeTable(x)
, but for some reason, even though the output is small, the query execution is not as fast. The query plan shows that 97% of the time is spent on Index Scan of hugeTable_by_x
, with an estimated number of rows equal to the size of the entire table. This is followed by, among other things, a Hash Match operation.
Since I created an index on column x
, can I not expect this query to run very quickly?
Note that I'm using Microsoft SQL Server 2005.