Guid.NewGuid().GetHashCode() 用于数据库

发布于 2024-08-22 04:23:07 字数 65 浏览 2 评论 0原文

这对于用作数据存储(SQL Server)的 ID 可靠吗?

我会使用 guid,但我更喜欢数字值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

孤独陪着我 2024-08-29 04:23:07

guid数值更有可能唯一表示一条记录。

同时:

  • GUID 确保全局唯一性
  • GUID 可以跨数据库移动
    GUID很好地
  • 减少了连接数量
    必需

请参阅:Guid 还是 Int 主键?

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?

橘和柠 2024-08-29 04:23:07

这对于用作数据存储(SQL Server)的 ID 可靠吗?

不会。GUID 是 128 位,但哈希码是 32 位。因此,必然存在碰撞。您可能不太可能遇到它,但不能保证您永远不会遇到它。

您想要的可靠性是保证您永远不会遇到碰撞。如果您坚持使用 Guid.NewGuid().GetHashCode() ,那么您需要添加逻辑来检测冲突。 GUID 确实有优点(和缺点),但如果没有其他信息,我建议使用自动递增 int 列。特别是当你说你想要一个数字列时,我倾向于使用 IDENTITY

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.

安穩 2024-08-29 04:23:07

真正的 GUID 被设计为唯一的。当您将其减少为 int (通过 GetHashCode)时,它唯一的概率就会降低。

使用 GUID(唯一性)有一个很好的理由,而此代码删除了该 GUID 功能。

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.

假情假意假温柔 2024-08-29 04:23:07

如果您想要一个数值,请使用 IDENTITY 列。如果您需要 GUID,请使用uniqueidentifier。就这么简单。

不要尝试混合搭配。不要对 GUID 进行哈希处理来获取数值。这将使您面临 GUID 列的所有缺点(更大的数据/索引、页面拆分),同时激发大部分优点(实际唯一性、复制支持)。此外,您无法获得顺序数字 ID 所能提供的任何优势,例如时间排序和索引性能。

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.

风尘浪孓 2024-08-29 04:23:07

我想说只使用 GUID 作为列上的值。然后就没有问题了。

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

风吹雪碎 2024-08-29 04:23:07
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

好吧,我想毕竟它几乎一样好。

没有碰撞:D。

50000000 个循环在 500 个线程上是相当重的。这对我来说已经足够好了。

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.

溺深海 2024-08-29 04:23:07

这是一种常见的方法,我将立即列出一个走这条路的好理由。您可以在访问 DB 之前生成 GUID,这样您就可以异步执行插入操作,并且您会提前知道 ID 是什么。

确保主键的数据类型是 UNIQUEIDENTIFIER 类型,并且一切就绪。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文