clustered index for materialized view

  • I have a view that joins a dozen tables with a million rows added per year by an application. I want to materialize it. The view is always filtered by date first on reports, then there are a few key transaction keys, but then many other fields required to make each row unique. I don't want to add these columns since they are large, many, not used for sorting or filtering, and may not define uniqueness in a future application design. I need a uniqueifier that is application agnostic. I prefer a bigint. So to store the materialized view ideally for reporting, I want to add the following clustered index to materialize the view:

    CREATE unique CLUSTERED INDEX idx1

    ON [dbo].[myview](myDate, key1, key2, key3, id bigint identity(1,1) NOT NULL)

    And I get this error:

    Msg 102, Level 15, State 1, Line 3

    Incorrect syntax near 'bigint'.

    Can I do what I want? If so, how? If not, a good alternative? Thanks!

  • An index is defined on existing columns, you can't create a new column as part of an index definition.

    Either ensure that there's an identity pulled through from the tables that you can use to make the clustered index unique, or define columns that are unique.

    Just note the limit for an index key is 900 bytes or 16 columns, and that indexed views have a whole massive pile of restrictions.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply