Slow distinct query in SQL Server over large dataset

前端 未结 10 2607
情深已故
情深已故 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 00:58

    As others have already pointed out - when you do a SELECT DISTINCT (typename) over your table, you'll end up with a full table scan no matter what.

    So it's really a matter of limiting the number of rows that need to be scanned.

    The question is: what do you need your DISTINCT typenames for? And how many of your 200M rows are distinct? Do you have only a handful (a few hundred at most) distinct typenames??

    If so - you could have a separate table DISTINCT_TYPENAMES or something and fill those initially by doing a full table scan, and then on inserting new rows to the main table, just always check whether their typename is already in DISTINCT_TYPENAMES, and if not, add it.

    That way, you'd have a separate, small table with just the distinct TypeName entries, which would be lightning fast to query and/or to display.

    Marc

提交回复
热议问题