Changing newid() to newsequentialid() on an existing table

后端 未结 4 2079
鱼传尺愫
鱼传尺愫 2021-02-04 21:13

At the moment we have a number of tables that are using newid() on the primary key. This is causing large amounts of fragmentation. So I would like to change the column to use n

4条回答
  •  遥遥无期
    2021-02-04 21:38

    You might think about using comb guids, as opposed to newsequentialid.

    cast(
        cast(NewID() as binary(10)) +
        cast(GetDate() as binary(6))
    as uniqueidentifier)
    

    Comb guids are a combination of purely random guids along with the non-randomness of the current datetime, so that sequential generations of comb guids are near each other and in general in ascending order. Comb guids have various advantages over newsequentialid, including the facts that they are not a black box, that you can use this formula outside of a default constraint, and that you can use this formula outside of SQL Server.

提交回复
热议问题