views:

380

answers:

7

Would this be reliable for using as an ID for data storage(SQL Server)?

I would use a guid but I prefer a numeric value.

A: 

I'd say just use a GUID as the value on the column. Then no issues.

Preet Sangha
+3  A: 

A guid is more likely to represent a record uniquely than a numeric value.

Along with:

  • GUIDs ensure global uniqueness
  • GUIDs can be moved across databases nicely
  • GUIDs reduce the number of joins required

See this : Guid Or Int Primary Key?

Asad Butt
+2  A: 

Would this be reliable for using as an ID for data storage(SQL Server)?

No. GUIDs are 128-bit but hashcodes are 32-bit. Therefore, there are necessarily collisions. It may be unlikely that you ever encounter one, but you are not guaranteed to never encounter one.

What you want for reliability is a guarantee that you never encounter a collision. If you insist on using Guid.NewGuid().GetHashCode() then you need to add logic to detect collisions. GUIDs do have advantages (and disadvantages) but without additional information I would suggest using an auto-incrementing int column. Especially as you say you want a numeric column I would lean towards using an IDENTITY.

Jason
+3  A: 

A real GUID is designed to be unique. When you reduce that to an int (via GetHashCode) the probability of it being unique is reduced.

There is one good reason to use GUIDs (uniqueness) and this code removes that GUID feature.

Arve
Auto-incrementing `int` are unique too so saying merely the same about GUID is not good enough of a reason.
Jason
True, and auto-incremental int is also unique. But that requires getting the unique int from some central place.The GUIDs feature is that I can get a new GUID without checking with some central place and it will be unique.
Arve
+2  A: 

If you want a numeric value then use an IDENTITY column. If you want a GUID, then use a uniqueidentifier. Simple as that.

Don't try to mix and match. Don't hash a GUID to get a numeric value. That will leave you with all of the disadvantages of a GUID column (larger data/indexes, page splits) while stimying most of the advantages (actual uniqueness, replication support). In addition you get none of the advantages that a sequential numeric ID would give you, such as temporal ordering and index performance.

Aaronaught
A: 

This is a common approach and I'll name one good reason right off hand for going this route. You can generate the GUID before you hit the DBso you could execute say an insert asynchronously and you would know ahead of time what the ID will be.

Make sure you're primary key's data type is a UNIQUEIDENTIFIER type and you're all set.

used2could
A: 
Dim bom As New Dictionary(Of Long, Boolean)

Sub pageload() Handles Me.Load
    For i = 0 To 500
        Dim act As New Action(AddressOf collisionfind)
        act.BeginInvoke(Nothing, Nothing)
    Next
End Sub

Sub collisionfind()
    For index = 1 To 50000000
        Dim INTGUID = Guid.NewGuid.GetHashCode / 2 * Guid.NewGuid.GetHashCode / 2
        bom.Add(INTGUID, Nothing)
    Next
End Sub

Well I guess after all it is almost as good.

No collisions :D.

50000000 Loops on 500 threads is quite heavy. It's good enough for me.

diamandiev