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

后端 未结 4 2075
鱼传尺愫
鱼传尺愫 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:33

    If you switch to sequentialguids and reorganize the index once at the same time, you'll eliminate fragmentation. I don't understand why you want to just wait until the fragmented page links rearrange themselves in continuous extents.

    That being said, have you done any measurement to show that the fragmentation is actually affecting your system? Just looking at an index and seeing 'is fragmented 75%' does not imply that the access time is affected. There are many more factors that come into play (buffer pool page life expectancy, rate of reads vs. writes, locality of sequential operations, concurrency of operations etc etc). While switching from guids to sequential guids is usualy safe, you may introduce problems still. For instance you can see page latch contention for an insert intensive OLTP system because it creates a hot-spot page where the inserts accumulate.

    0 讨论(0)
  • 2021-02-04 21:33

    Thank you, yfeldblum! Your simple and concise explanation of COMB GUIDs really helped me out. I was actually looking at doing the reverse of this post: I had to get away from relying on newsequentialid() since I was trying to migrate a SQL Server 2012 db to Azure, and the newsequentialid() function is not supported there.

    I was able to change all of my table PK defaults to COMB GUIDs, with the following syntax:

    ALTER TABLE [dbo].[Company] 
    ADD  CONSTRAINT [DF__Company__Company_ID__72E6D332]  
        DEFAULT (CONVERT([uniqueidentifier],CONVERT([binary](10),newid(),0)+CONVERT([binary](6),getdate(),0),0)) FOR [CompanyId]
    GO
    

    My SQL2012 db is now happily living in the Azure cloud.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 21:40

    If this is SQL Server, you are generating a Guid by calling newid(). This is not good for primary keys. Use an integer identity column for the primary key, and make your Guid a surrogate key (and a row guid column).

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