2

We use Guid's as identifiers for at least half of our tables. For the other tables more 'natural' identifiers could be used so we could profit from a natural clustered index. We noticed that the usage of the Guids causes a lot of overhead in the database since they are not sequential.

Has anyone implemented Sequential Guids in Codefluent, or tried? Can you share your approach and/or code?

Gustav
  • 53,498
  • 7
  • 29
  • 55
Peter de Bruijn
  • 792
  • 6
  • 22
  • A "Sequential Guid" is a contradiction. So no. – Gustav Feb 13 '17 at 08:39
  • Gustav, there is a lot of documentation on sequential guids. Please read http://www.siepman.nl/blog/post/2013/10/28/ID-Sequential-Guid-COMB-Vs-Int-Identity-using-Entity-Framework.aspx for instance. – Peter de Bruijn Feb 13 '17 at 08:50
  • 1
    Thanks, didn't know about that. Seems to be an _SQL Server_ special, so I added the _SQL Server_ tag to your question. – Gustav Feb 13 '17 at 08:59
  • A clustered index on a guid is a non-sense. Clustered should be put carefully where (pseudo) physical ordering makes sense. If you use guid (which can make sense if you want unique ids across time and space), don't define it as clustered, if you want clustered (which can make sense depending on your queries), don't use guids. – Simon Mourier Feb 13 '17 at 09:09
  • Gustav, in SQL server the database can add the sequential GUID indeed. However that defeats part of the purpose of a GUID (that the business object layer can create the identifier). – Peter de Bruijn Feb 13 '17 at 09:24

1 Answers1

2

You can generate a new sequential guid in C# with UuidCreateSequential and assign it to the property in the constructor (OnAfterCreate rule).

The CodeFluent Entities model:

<cf:entity name="Customer">
  <cf:property name="Id" cfom:newGuidOnNew="false" key="true" />
  <cf:property name="Name" />
  <cf:rule typeName="OnAfterCreate" />
</cf:entity>

The custom code to generate a Sequential guid:

partial class Customer
{
    void OnAfterCreate()
    {
        Id = SequentialGuid.NewGuid();
    }
}

static class SequentialGuid
{
    [DllImport("rpcrt4.dll", EntryPoint = "UuidCreateSequential")]
    public static extern Guid NewGuid();
}
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
meziantou
  • 20,589
  • 7
  • 64
  • 83
  • Thanks meziantou, that is what I was looking for! – Peter de Bruijn Feb 13 '17 at 09:33
  • @PeterdeBruijn - This won't change a lot, it needs to be improved: https://blogs.msdn.microsoft.com/dbrowne/2012/07/03/how-to-generate-sequential-guids-for-sql-server-in-net/ but anyway I don't see how it will be "sequential" over a period of time, and from different machines. If one needs "sequentiality", one must use identity not guids – Simon Mourier Feb 14 '17 at 07:30