Slow distinct query in SQL Server over large dataset

前端 未结 10 2605
情深已故
情深已故 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:56

    An index helps you quickly find a row. But you're asking the database to list all unique types for the entire table. An index can't help with that.

    You could run a nightly job which runs the query and stores it in a different table. If you require up-to-date data, you could store the last ID included in the nightly scan, and combine the results:

    select type
    from nightlyscan
    union
    select distinct type
    from verybigtable
    where rowid > lastscannedid
    

    Another option is to normalize the big table into two tables:

    talbe1: id, guid, typeid
    type table: typeid, typename
    

    This would be very beneficial if the number of types was relatively small.

提交回复
热议问题