最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[整理]数据库设计主键时UUID和GUID的区别

database crifan 4529浏览 0评论

折腾:

[已解决]SQLAlchemy中给列的id从数字变成UUID

期间,需要知道:

xxxx-xxxx-xxxx-xxxx

xxxxxxxxxxxxxxxx

到底什么是GUID还是UUID

数据库 GUID UUID

UUID 和 GUID 的区别 – ChaunceyHao – 博客园

“UUID是一个由4个连字号(-)将32个字节长的字符串分隔后生成的字符串,总共36个字节长。比如:550e8400-e29b-41d4-a716-446655440000

GUID 是微软对UUID这个标准的实现。UUID是由开放软件基金会(OSF)定义的。UUID还有其它各种实现,不止GUID一种。比如我们这里在Java中用到的。”

GUID UUID生成主键策略_数据库技术_Linux公社-Linux系统门户网站

“GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值。

GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。例如:76895313-839E-4E89-BAFC-B253BFF3173F

世界上的任何两台计算机都不会生成重复的 GUID 值。GUID 主要用于在拥有多个节点、多台计算机的网络或系统中,分配必须具有唯一性的标识符。在 Windows 平台上,GUID 应用非常广泛:注册表、类及接口标识、数据库、甚至自动生成的机器名、目录名等。”

数据库使用 UUID 做主键

“UUID是是128位整数(16字节)的全局唯一标识符(Universally Unique Identifier),指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。通常平台会提供生成UUID的API。UUID按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。由以下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相同),时钟序列,全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得),UUID的唯一缺陷在于生成的结果串会比较长。关于UUID这个标准使用最普遍的是微软的GUID(Globals Unique Identifiers)。

UUID,其格式为:xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx(8-4-4-16) ,其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。

而标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)

归纳起来,对主键的选择主要有以下四种方式:

自动增长字段 自动增长型字段允许我们在向数据库添加数据时,不考虑主键的取值,记录插入后,数据库系统会自动为其分配一个值,确保绝对不会出现重复。

手动增长字段 手动增长型的字段,也就是说主键的值需要自己维护,通常情况下需要建立一张单独的表存储当前主键键值。

GUID类型 GUID是Globally Unique IDentifier的缩写,是一个128位的随机数,并保证不产生重复。

COMB类型 COMB(combine)型可以理解为一种改进的GUID,它通过组合GUID和系统时间,以使其在索引和检索事有更优的性能。”

GUID / UUID数据库关键字的优点和缺点 _Advantages and disadvantages of GUID / UUID database keys_database_帮酷问答

Globally unique identifier – Wikipedia, the free encyclopedia

“A globally unique identifier (GUID, /ˈɡwɪd/[citation needed] or /ˈɡuːɪd/) is a unique reference number used as an identifier in computer software. The term “GUID” typically refers to various implementations of the universally unique identifier (UUID) standard.[1]”

Universally unique identifier – Wikipedia, the free encyclopedia

“A universally unique identifier (UUID) is an identifier standard used in software construction. A UUID is simply a 128-bit value. The meaning of each bit is defined by any of several variants.”

UUID与int作主键的优缺点比较-数据库-易习特达网

 GUID vs UUID

What is the difference between UUID and GUID? – Quora

“UUID is an OSF standard. GUID refers to certain (mostly Microsoft) implementations of that standard. In other words, no real difference. A GUID is a UUID, but not necessarily vice versa.”

Primary Keys: IDs versus GUIDs

GUID Pros
Unique across every table, every database, every server
Allows easy merging of records from different databases
Allows easy distribution of databases across multiple servers
You can generate IDs anywhere, instead of having to roundtrip to the database
Most replication scenarios require GUID columns anyway
GUID Cons
It is a whopping 4 times larger than the traditional 4-byte index value; this can have serious performance and storage implications if you’re not careful
Cumbersome to debug where userid='{BAE7DF4-DDF-3RG-5TY3E3RF456AS10}’
The generated GUIDs should be partially sequential for best performance (eg, newsequentialid() on SQL 2005) and to enable use of clustered indexes

Is there any difference between a GUID and a UUID? – Stack Overflow

GUID is Microsoft’s implementation of the UUID standard.

The term GUID usually refers to Microsoft’s implementation of the Universally Unique Identifier (UUID) standard.”

[解釋] uuid 與 guid 的差異

[总结]

UUID:是个标识符定义方面的软件标准,定义了一个128bit的值

注:具体是Open Software Foundation (OSF)去实现标准化的,作为了 Distributed Computing Environment (DCE)的其中一部分。

对应标准和协议:

 ISO/IEC 11578:1996 “Information technology – Open Systems Interconnection – Remote Procedure Call (RPC)

ITU-T Rec. X.667

ISO/IEC 9834-8:2005

GUID:是UUID标准的其中一种实现。目前使用最广。决定了用什么策略,生成这个32字节的字母数字。

常见的写法是:

8位-4位-4位-4位-12位

一共32位

比如:

e85906ff-aca6-457f-af46-793e46b51c71

常见用途:

  • 微软:用于内部COM对象和接口的标识
  • Intel:GUID分区表
  • 数据库开发者们:常用于数据库的主键key value,以保证主键的唯一性和不会冲突

转载请注明:在路上 » [整理]数据库设计主键时UUID和GUID的区别

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.168 seconds, using 22.10MB memory