GUID/UUID 数据库键的优缺点 [英] Advantages and disadvantages of GUID / UUID database keys

查看:35
本文介绍了GUID/UUID 数据库键的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去曾在许多数据库系统上工作过,如果所有数据库键都是 GUID/UUID 值.我曾多次考虑沿着这条路走下去,但始终存在一些不确定性,尤其是在性能和​​无法通过电话读取的 URL 方面.

I've worked on a number of database systems in the past where moving entries between databases would have been made a lot easier if all the database keys had been GUID / UUID values. I've considered going down this path a few times, but there's always a bit of uncertainty, especially around performance and un-read-out-over-the-phone-able URLs.

有没有人在数据库中广泛使用过 GUID?这样做我会得到什么好处,可能的陷阱是什么?

Has anyone worked extensively with GUIDs in a database? What advantages would I get by going that way, and what are the likely pitfalls?

推荐答案

优点:

  • 可以离线生成它们.
  • 使复制变得微不足道(与 int 相比,这使它变得非常困难)
  • ORM 通常喜欢它们
  • 在应用程序中独一无二.所以我们可以在我们的应用程序(也是 guid)中使用来自我们的 CMS (guid) 的 PK 并且知道我们永远不会发生冲突.

缺点:

  • 更大的空间使用,但空间便宜(呃)
  • 无法按 ID 订购以获取插入顺序.
  • 在 URL 中可能看起来很丑陋,但实际上,WTF 您是否正在将 REAL DB 键放入 URL 中!?(这一点在下面的评论中有争议)
  • 手动调试比较难,但没那么难.

就我个人而言,我将它们用于任何大小合适的系统中的大多数 PK,但我在一个到处复制的系统上接受了培训",因此我们必须拥有它们.天啊.

Personally, I use them for most PK's in any system of a decent size, but I got "trained" on a system which was replicated all over the place, so we HAD to have them. YMMV.

我认为重复数据的事情是垃圾——你可以得到重复的数据,不管你怎么做.在我工作过的地方,代理键通常不受欢迎.不过,我们确实使用了类似 WordPress 的系统:

I think the duplicate data thing is rubbish - you can get duplicate data however you do it. Surrogate keys are usually frowned upon where ever I've been working. We DO use the WordPress-like system though:

  • 行的唯一 ID(GUID/任何).永远不会对用户可见.
  • 公共 ID 从某个字段生成一次(例如标题 - 使其成为文章的标题)

更新:所以这个得到了很多 +1,我想我应该指出 GUID PK 的一个很大的缺点:聚集索引.

UPDATE: So this one gets +1'ed a lot, and I thought I should point out a big downside of GUID PK's: Clustered Indexes.

如果您有很多记录,并且在 GUID 上有聚集索引,那么您的插入性能会很糟糕,因为您在项目列表中的随机位置插入(这就是重点),而不是在最后(这是快)

If you have a lot of records, and a clustered index on a GUID, your insert performance will SUCK, as you get inserts in random places in the list of items (thats the point), not at the end (which is quick)

因此,如果您需要插入性能,可以使用 auto-inc INT,如果您想与其他人共享(即,在 URL 中向用户显示)并生成 GUID

So if you need insert performance, maybe use a auto-inc INT, and generate a GUID if you want to share it with someone else (ie, show it to a user in a URL)

这篇关于GUID/UUID 数据库键的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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