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

后端 未结 8 821
闹比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条回答
  •  萌比男神i
    2021-01-01 12:02

    This is likely not a problem of indexing, but one of data design. Normalization, to be precise. The fact that you need to query distinct values of a field, and even willing to add an index, is a strong indicator that the field should be normalized into a separate table with a (small) join key. Then the distinct values will be available immediately by scanning the much smaller lookup foreign table.

    Update
    As a workaround, you can create an indexed view on an aggregate by the 'distinct' field. COUNT_BIG is an aggregate that is allowed in indexed views:

    create view vwDistinct
    with schemabinding
    as select x, count_big(*)
    from schema.hugetable
    group by x;
    
    create clustered index cdxDistinct on vwDistinct(x);
    
    select x from vwDistinct with (noexpand);
    

提交回复
热议问题