Guid.NewGuid().GetHashCode() 用于数据库
这对于用作数据存储(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
guid
比数值
更有可能唯一表示一条记录。同时:
GUID很好地
必需
请参阅:Guid 还是 Int 主键?
A
guid
is more likely to represent a record uniquely than anumeric value
.Along with:
nicely
required
See this : Guid Or Int Primary Key?
不会。GUID 是 128 位,但哈希码是 32 位。因此,必然存在碰撞。您可能不太可能遇到它,但不能保证您永远不会遇到它。
您想要的可靠性是保证您永远不会遇到碰撞。如果您坚持使用 Guid.NewGuid().GetHashCode() ,那么您需要添加逻辑来检测冲突。 GUID 确实有优点(和缺点),但如果没有其他信息,我建议使用自动递增
int
列。特别是当你说你想要一个数字列时,我倾向于使用IDENTITY
。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-incrementingint
column. Especially as you say you want a numeric column I would lean towards using anIDENTITY
.真正的 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.
如果您想要一个数值,请使用
IDENTITY
列。如果您需要 GUID,请使用uniqueidentifier
。就这么简单。不要尝试混合搭配。不要对 GUID 进行哈希处理来获取数值。这将使您面临 GUID 列的所有缺点(更大的数据/索引、页面拆分),同时激发大部分优点(实际唯一性、复制支持)。此外,您无法获得顺序数字 ID 所能提供的任何优势,例如时间排序和索引性能。
If you want a numeric value then use an
IDENTITY
column. If you want a GUID, then use auniqueidentifier
. 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.
我想说只使用 GUID 作为列上的值。然后就没有问题了。
I'd say just use a GUID as the value on the column. Then no issues.
好吧,我想毕竟它几乎一样好。
没有碰撞:D。
50000000 个循环在 500 个线程上是相当重的。这对我来说已经足够好了。
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.
这是一种常见的方法,我将立即列出一个走这条路的好理由。您可以在访问 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.