在关系数据库中使用 UUID 列与递增 ID 和单独的 UUID 列 (Laravel, Eloquent)
Using a UUID column vs. and incrementing ID and a separate UUID column in relational databases (Laravel, Eloquent)
我经常看到两种方法:
1: UUID 作为 ID
Schema::create('orders', function (Blueprint $table) {
$table->uuid('id')->unique();
...
2:UUID和自增ID
Schema::create('orders', function (Blueprint $table) {
$table->bigIncrements('id');
$table->uuid('uuid')->unique();
...
这两种方法有什么显着的好处吗?我认为可以看到选项的一个好处是预加载。例如:
select * from `order_addresses` where `order_addresses`.`id` in (1, 2, 3, 4, 5)
对比
select * from `order_addresses` where `order_addresses`.`id` in ('683d3bc7-edd7-4f12-a7eb-831bfc5e90eb','20d3d3f5-2b0d-45e0-9f17-f581317b3f97','907af98b-e433-4e55-a641-3f134ea9039c','7713462c-b8aa-4d11-a576-7d4634595a35','4a27368e-5ebe-43e4-bfaf-8be303a84318','e5e618d9-fd25-4f98-bc70-03bc378c338d','5aa3dd71-a4fc-44ac-a810-2e414372d1ed','9c62bbdc-2555-4239-81fd-365ada304619','a7f22427-b7e7-41c0-bc38-f84306f0bae6','386d8318-3da5-4de1-95d0-f144b53ed76d')
然而我不是100%。任何人都可以提供任何进一步的论据 and/or 验证上述内容吗?
UUID 更适合 n 层应用程序,其中每个应用程序都可以创建自己的标识符,而没有违反 "unique" 规则的风险。
无符号大整数和UUID两者的速度差异很小
UUID使用较多space,仅此而已
A string-based UUID 在 MySQL(以及其他一些)的情况下确实会影响性能。
https://www.percona.com/blog/2019/11/22/uuids-are-popular-but-bad-for-performance-lets-discuss/
Laravel 社区中的一种常见方法似乎是使用 auto-incrementing 大整数作为主键,然后在旁边添加一个索引 UUID。
您还可以查看可用的二进制 UUID 包,它可以更有效地在数据库中存储 UUID。
我经常看到两种方法:
1: UUID 作为 ID
Schema::create('orders', function (Blueprint $table) {
$table->uuid('id')->unique();
...
2:UUID和自增ID
Schema::create('orders', function (Blueprint $table) {
$table->bigIncrements('id');
$table->uuid('uuid')->unique();
...
这两种方法有什么显着的好处吗?我认为可以看到选项的一个好处是预加载。例如:
select * from `order_addresses` where `order_addresses`.`id` in (1, 2, 3, 4, 5)
对比
select * from `order_addresses` where `order_addresses`.`id` in ('683d3bc7-edd7-4f12-a7eb-831bfc5e90eb','20d3d3f5-2b0d-45e0-9f17-f581317b3f97','907af98b-e433-4e55-a641-3f134ea9039c','7713462c-b8aa-4d11-a576-7d4634595a35','4a27368e-5ebe-43e4-bfaf-8be303a84318','e5e618d9-fd25-4f98-bc70-03bc378c338d','5aa3dd71-a4fc-44ac-a810-2e414372d1ed','9c62bbdc-2555-4239-81fd-365ada304619','a7f22427-b7e7-41c0-bc38-f84306f0bae6','386d8318-3da5-4de1-95d0-f144b53ed76d')
然而我不是100%。任何人都可以提供任何进一步的论据 and/or 验证上述内容吗?
UUID 更适合 n 层应用程序,其中每个应用程序都可以创建自己的标识符,而没有违反 "unique" 规则的风险。
无符号大整数和UUID两者的速度差异很小
UUID使用较多space,仅此而已
A string-based UUID 在 MySQL(以及其他一些)的情况下确实会影响性能。
https://www.percona.com/blog/2019/11/22/uuids-are-popular-but-bad-for-performance-lets-discuss/
Laravel 社区中的一种常见方法似乎是使用 auto-incrementing 大整数作为主键,然后在旁边添加一个索引 UUID。
您还可以查看可用的二进制 UUID 包,它可以更有效地在数据库中存储 UUID。