Question

I have the following situation: I'm creating a database that will work locally, but can be migrate to a online database that have the same schema and will already have some data. For this reason, I'm planning to use GUIDs as the primary keys for it's tables, as it will make migrations easier by avoiding the need to change primary key values (and cascading it to referencing tables) that would occur if I use any identity column (am I right?). The database is build on SQL Server 2012.

Despite this, I would like to have a clustered index to speed up queries on the database tables, and as I read many articles and Stack Overflow answers, I'm convinced that having a clustered index on a GUID primary key is not a good idea. So, the question is: is there any good solution to have a clustered index on this database tables? I'm open to removing the GUIDs and adding another data type. For a second I thought of using a HiLo approach, but I think that it would make the migration harder, which is an option, but I need a really good reason to choose it (namelly, if there is no good way to use a GUID ad PK and speed up queries).

Until now I thought of these solutions:

  • Using newsequentialid() to generate the new GUIDs, making them sequential and better as a clustered index. The question here is: is this really any better than using a int primary key? The main reason here to use GUIDs is to help with any migration that will eventually happen, and I think that making them sequencial would not help here. Plus, I would have to retrieve the db generated GUID instead of create it on the client side.
  • Using a COMB GUID and making it also the clustered index. It seems to solve some problems that exist in the previous approach, while keeping a high "randomness factor", but I don't know if there are any outcomes. Is there?
  • Adding a identity int column with and using it as the clustered index. My question here is if it will help on anything, since the queries will be made with the PK value anyway, which is a GUID.
  • Slightly chance the previous approach, turning the identity int into the primary key value and clustered index, and adding a GUID collumn with which I would make the queries. I see that on Adventure Works 2012 this is the choice, but I don't really understand where it helps... will the clustered index help if I query the values with the GUID, which is not the PK nor clustered?

I think thats all that I could come up with, and I'm really inclined to choose the COMB approach, and thinking on some experiments to validate if and why it's better. But is there a better solution in this scenario?

Was it helpful?

Solution

// Plus, I would have to retrieve the db generated GUID instead of create it on the client side.//

((( Note, I wanted to put this as a comment, but the "code" was too big for a simple comment. But this ~could be an answer. )))

You can create a client side (mostly) sequential guid..... As more of a C# developer than a DBA, I like to create all my relationships outside the database, and then send everything down as Xml, let TSQL shred it, and then Upsert everything I need in one db hit (an occasional delete as well I guess). But there are shortcoming to this approach. I think int and bigint will usually "win" the performance contests......BUT........most of the time.........using guid with the client side "sequential" is plenty good enough for my needs. Especially, as you suggest...you have to move that data at some point. Getting PK/FK's to "lineup" using IDENTITY() is doable, but easier with GUID's (IMHO). Now, if I were writing T~ick3t M@ster or an Am@zing online retail website, I would not pick GUID.

Good luck.

using System;
using System.Runtime.InteropServices;


namespace MyCompany.MyTechnology.Framework.CrossDomain.GuidExtend
{
    public static class Guid
    {

        /*

        Original Reference for Code:
        http://www.pinvoke.net/default.aspx/rpcrt4/UuidCreateSequential.html

        */


        [DllImport("rpcrt4.dll", SetLastError = true)]
        static extern int UuidCreateSequential(out System.Guid guid);

        public static System.Guid NewGuid()
        {
            return CreateSequentialUuid();
        }


        public static System.Guid CreateSequentialUuid()
        {
            const int RPC_S_OK = 0;
            System.Guid g;
            int hr = UuidCreateSequential(out g);
            if (hr != RPC_S_OK)
                throw new ApplicationException("UuidCreateSequential failed: " + hr);
            return g;
        }


        /*

        Text From URL above:

        UuidCreateSequential (rpcrt4)

        Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
        To create a page in a module other than rpcrt4, prefix the name with the module name and a period.
        . Summary
        Creates a new UUID 
        C# Signature:
        [DllImport("rpcrt4.dll", SetLastError=true)]
        static extern int UuidCreateSequential(out Guid guid);


        VB Signature:
        Declare Function UuidCreateSequential Lib "rpcrt4.dll" (ByRef id As Guid) As Integer


        User-Defined Types:
        None.

        Notes:
        Microsoft changed the UuidCreate function so it no longer uses the machine's MAC address as part of the UUID. Since CoCreateGuid calls UuidCreate to get its GUID, its output also changed. If you still like the GUIDs to be generated in sequential order (helpful for keeping a related group of GUIDs together in the system registry), you can use the UuidCreateSequential function.

        CoCreateGuid generates random-looking GUIDs like these:

        92E60A8A-2A99-4F53-9A71-AC69BD7E4D75
        BB88FD63-DAC2-4B15-8ADF-1D502E64B92F
        28F8800C-C804-4F0F-B6F1-24BFC4D4EE80
        EBD133A6-6CF3-4ADA-B723-A8177B70D268
        B10A35C0-F012-4EC1-9D24-3CC91D2B7122



        UuidCreateSequential generates sequential GUIDs like these:

        19F287B4-8830-11D9-8BFC-000CF1ADC5B7
        19F287B5-8830-11D9-8BFC-000CF1ADC5B7
        19F287B6-8830-11D9-8BFC-000CF1ADC5B7
        19F287B7-8830-11D9-8BFC-000CF1ADC5B7
        19F287B8-8830-11D9-8BFC-000CF1ADC5B7



        Here is a summary of the differences in the output of UuidCreateSequential:

        The last six bytes reveal your MAC address 
        Several GUIDs generated in a row are sequential 
        Tips & Tricks:
        Please add some!

        Sample Code in C#:
        static Guid UuidCreateSequential()
        {
           const int RPC_S_OK = 0;
           Guid g;
           int hr = UuidCreateSequential(out g);
           if (hr != RPC_S_OK)
             throw new ApplicationException
               ("UuidCreateSequential failed: " + hr);
           return g;
        }



        Sample Code in VB:
        Sub Main()
           Dim myId As Guid
           Dim code As Integer
           code = UuidCreateSequential(myId)
           If code <> 0 Then
             Console.WriteLine("UuidCreateSequential failed: {0}", code)
           Else
             Console.WriteLine(myId)
           End If
        End Sub




        */








    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top