为什么使用应用程序生成自定义uuid而不是数据库ID? [英] why use apps for generating custom uuid instead database id?

查看:148
本文介绍了为什么使用应用程序生成自定义uuid而不是数据库ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我应该使用应用程序而不是数据库ID来生成UUID?有优势吗?

why should I use an app to generate UUID instead using database id's? is there an advantage?

我看到许多生成防止细菌感染的UUID的应用程序。因此,是否有可能在像mysql或mongodb这样的数据库ID中发生这种情况?

I see many apps that generates UUID preventing colision. so than is this possible to happen in database id like mysql or mongodb?

推荐答案

使用理论上的原因而不是依赖UUID数据库ID上的功能是可以并行插入新记录,而无需锁定和递增计数器,甚至不查找唯一索引。

One theoretical reason for using random UUIDs instead of relying on database IDs is the ability to do parallel inserts of new records without locking and incrementing a counter, or even looking up a unique index.

如果您具有自动递增功能字段,需要维护此计数器。您可以确保自动递增功能正常工作的唯一方法是在递增计数器时锁定该计数器,或者对其进行其他事务事务处理。如果您有一个随机ID,则不必这样做。

If you have an auto-increment field, something needs to maintain this counter. The only way you can guarantee that the auto-increment facility will work correctly is by locking this counter while incrementing it, or doing something else transaction-y with it. If you have a random ID, you don't have to do it.

另一个原因是可以在主-主环境中执行并行分布式插入。当不同的数据库服务器访问相同的数据时,维护两个服务器之间的共享计数器会更加昂贵,因此最好一开始就放弃并使用随机标识符。

Another reason is the ability to perform parallel distributed inserts in a master-master environment. When different database servers access the same data, it would be even more expensive to maintain a shared counter between the two servers, so better to just give it up from the start and use random identifiers.

但是这些原因在实践中很少适用。您将需要大量的INSERT流量才能获得回报。我猜大多数UUID的使用不是因为必要,而是因为酷或对它们真正用途的无知。

But these reasons are very rarely applicable in practice. You would need huge volumes of INSERT traffic to reach payoff. I guess most UUID usage is not because of necessity but because of "coolness" or ignorance towards what they really are for.

这篇关于为什么使用应用程序生成自定义uuid而不是数据库ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆