“每个表”上的自动增量键的优点和缺点 [英] Pros and Cons of autoincrement keys on "every table"

查看:93
本文介绍了“每个表”上的自动增量键的优点和缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在我们公司里讨论了是否在我们的数据库中的每个表上放置一个自动增量密钥。

We are having a rather long discussion in our company about whether or not to put an autoincrement key on EVERY table in our database.

我可以理解表中有一个FK参考,但是我不喜欢把这些键放在我们每个表的每个表上,尽管这些键永远不会被使用。

I can understand putting one on tables that would have a FK reference to, but I kind-of dislike putting such keys on each and every one of our tables, even though the keys would never be used.

请帮助优点和缺点,将自动增量键放在每张桌子上,除了占用额外的空间,并减缓一切(我们有几个表格,数亿个记录)。

Please help with pros and cons for putting autoincrement keys on every table apart from taking extra space and slowing everything a little bit (we have some tables with hundreds of millions of records).

谢谢

推荐答案

我假设几乎所有的表都将有一个主键 - 这只是一个问题,该键由一个或多个自然键或单个自动递增代理键组成。如果你没有使用主键,那么你通常会在几乎所有的表格上使用它们的很多优点。

I'm assuming that almost all tables will have a primary key - and it's just a question of whether that key consists of one or more natural keys or a single auto-incrementing surrogate key. If you aren't using primary keys then you will generally get a lot of advantages of using them on almost all tables.

所以,这里有一些优点代理键的缺点。首先,优点:

So, here are some pros & cons of surrogate keys. First off, the pros:


  • 最重要的是:它们允许自然键改变。简单的例子,一个人的表应该有一个主键,而不是last_name,first_name。

  • 读取性能 - 非常小的索引扫描速度更快。但是,如果您实际上通过代理键限制您的查询,这只是有用的。因此,对于查找表来说,对于主表来说不是那么好。

  • 简单 - 如果适当地命名,则使数据库易于学习使用。

  • 容量 - 如果您正在设计类似数据仓库事实表 - 您的维度上的代理键,您可以保留一个非常狭窄的事实表 - 这将导致巨大的容量改进。

  • Most importantly: they allow the natural keys to change. Trivial example, a table of persons should have a primary key of person_id rather than last_name, first_name.
  • Read performance - very small indexes are faster to scan. However, this is only helpful if you're actually constraining your query by the surrogate key. So, good for lookup tables, not so good for primary tables.
  • Simplicity - if named appropriately, it makes the database easy to learn & use.
  • Capacity - if you're designing something like a data warehouse fact table - surrogate keys on your dimensions allow you to keep a very narrow fact table - which results in huge capacity improvements.

和cons:


  • 他们不能防止重复的自然价值。所以,你通常还需要在逻辑键上有一个唯一的约束(索引)。

  • 编写性能。使用额外的索引,您将减慢插入,更新和删除等等。

  • 简单 - 对于几乎不会更改的小数据表,它们是不必要的。例如,如果您需要国家/地区列表,则可以使用国家/地区的ISO列表。它包括有意义的缩写。这比代理键更好,因为它既小又实用。

通常,代理键是有用的,只是记住不要犹豫,在适当的时候使用自然键。

In general, surrogate keys are useful, just keep in mind the cons and don't hesitate to use natural keys when appropriate.

这篇关于“每个表”上的自动增量键的优点和缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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