SQL Server 中的 guid 实际上是如何存储和排序/比较的?
假设我有这个 guid:
{2A87E3E2-2B6A-4149-9F5A-1B76092843D9}
它实际上是否将其存储在数据库中的字母数字中? (我不这么认为,因为它必须适合 16 个字节。)
如果不适合,那么它是如何存储的? (我的猜测是十六进制数字,但我不确定。)
如何判断一个 GUID 是否大于另一个? (出于索引目的您可能需要了解哪些信息。)您是否只是读取 GUID像十六进制数字(用于比较)?
Say I have this guid:
{2A87E3E2-2B6A-4149-9F5A-1B76092843D9}
Does it actually store this an an alpha numeric in the database? (I don't think so cause it has to fit in 16 bytes.)
If it does not, then how is it stored? (My guess is as a hex number, but I am not sure.)
How can you tell if one GUID is greater than another? (Which you may need to know for indexing purposes.) Do you read the GUID just like a hex number (for comparison)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GUID 在内部存储为二进制 (16)。 MSDN 上的“使用 uniqueidentifier 数据” 告诉您这一点。
{
}
和-
不是值的一部分。GUID 可以“排序”并进行更大/更小的比较:请参阅规范 "SQL Server 如何对 GUID 进行排序?"。
注意:这意味着它们不按二进制(16)排序(除非您进行CAST,我想...)
我不明白为什么您想要这个现实生活(不是索引,我指的是现实世界):NEWID 的“排序”潜力的唯一用途是用于
ORDER BY NEWID()
的随机行技巧您可以拥有“升序”GUID (基于上面的文章)与 NEWSEQUENTIALID。然而,这种“排序”在 Windows 重新启动后不会继续 = IMO 毫无意义。
A GUID is stored as binary(16) internally. "Using uniqueidentifier Data" on MSDN tells you this.
The
{
}
and-
are not part of the value.GUIDs can be "sorted" and have greater/lesser comparisons: see the canonical "How are GUIDs sorted by SQL Server?".
Note: this means they don't sort as binary(16) (unless you CAST I suppose...)
I can't see why you'd want this in real life (not indexing, I mean real world): about the only use for the "sorting" potential of NEWID is for the random rows tricks of
ORDER BY NEWID()
You can have "ascending" GUIDs (based on the article above) with NEWSEQUENTIALID. However, this "sorting" doesn't continue after a Windows restart = pointless IMO.
死灵术。
GUID 存储为字节数组,即二进制 (16)。
您还可以将其存储为 UInt128 或两个 bigint。
关于GUID如何排序:
代码不言而喻,神奇的部分是
完整代码:
Necromancing.
The GUID is stored as byte-array, that is to say binary(16).
You could also store it as UInt128, or two bigints.
As to how a GUID is sorted:
The code speaks for itselfs, the magical parts are
Full code: