Slow distinct query in SQL Server over large dataset

前端 未结 10 2608
情深已故
情深已故 2021-02-14 00:27

We\'re using SQL Server 2005 to track a fair amount of constantly incoming data (5-15 updates per second). We noticed after it has been in production for a couple months that on

相关标签:
10条回答
  • 2021-02-14 01:00

    You do misunderstand the index. Even if it did use the index it would still do an index scan across 200M entries. This is going to take a long time, plus the time it takes to do the DISTINCT (causes a sort) and it's a bad thing to run. Seeing a DISTINCT in a query always raises a red flag and causes me to double check the query. In this case, perhaps you have a normalization issue?

    0 讨论(0)
  • 2021-02-14 01:01

    I should try something like this:

    SELECT typeName FROM [types] WITH (nolock)
    group by typeName;
    

    And like other i would say you need to normalize that column.

    0 讨论(0)
  • 2021-02-14 01:02

    I doubt SQL Server will even try to use the index, it'd have to do practically the same amount of work (given the narrow table), reading all 200M rows regardless of whether it looks at the table or the index. If the index on typeName was clustered it may reduce the time taken as it shouldn't need to sort before grouping.

    If the cardinality of your types is low, how about maintaining a summary table which holds the list of distinct type values? A trigger on insert/update of the main table would do a check on the summary table and insert a new record when a new type is found.

    0 讨论(0)
  • 2021-02-14 01:08

    I could be missing something but would it be more efficient if an overhead on load to create a view with distinct values and query that instead?

    This would give almost instant responses to the select if the result set is significantly smaller with the overhead over populating it on each write though given the nature of the view that might be trivial in itself.

    It does ask the question how many writes compared to how often you want the distinct and the importance of the speed when you do.

    0 讨论(0)
提交回复
热议问题