SQL Server 数据库中的 ULID 数据类型

问题描述 投票:0回答:1

在搜索

UUIDs
时,我偶然发现了
ULIDs
https://github.com/ulid/spec

我想在我的 SQL Server 数据库中使用它们作为我的用户的主键(而不是基本增量值)。

所以我想问:

  1. 什么数据类型最适合?

    uniqueidentifier
    binary16
    ?还有别的东西吗?为什么?

  2. 还有其他一些额外步骤可以充分受益于

    ULID
    结构吗?特别是排序/搜索?与其他
    UUIDs
    相比。

sql-server database t-sql uuid ulid
1个回答
0
投票

您需要一个 16 字节、全球唯一、可按时间顺序排序的唯一标识符。

SQL Server 已经具备了:

uniqueidentifier

很多人有一个错误的印象,认为

uniqueidentifer
是一个随机值,并且将其用作主键是不好的,因为它会导致IO分散,页面撕裂和时间局部性差。

这只适用于“随机”uuid - 称为“类型 4”uuid。

但是还有其他类型的 uuid - 特别是 1 类 UUID。它不是根据随机数据、MD5 或 SHA1 哈希构建的,而是基于当前时间戳构建的。

与如何将 uniqueidentifier 列默认为“类型 4 随机 uuid”类似:

CustomerUUID uniqueidentifier DEFAULT newid()

SQL Server 还支持串行类型 1 UUID:

CustomerUUID uniqueidentifier DEFAULT newsequentialid()
© www.soinside.com 2019 - 2024. All rights reserved.