简体   繁体   English

多个colums入门密钥与一个具有唯一约束的入门密钥

[英]multiple colums primery keys vs one primery key with unique constraints

I have large table on my database (50 columns and might get to 100,000,000 rows). 我的数据库上有大表(50列,可能达到100,000,000行)。 Right now my primary key is 8 columns. 现在,我的主键是8列。

It is better to make 1 primary key (automatic number) and add columns unique constrains? 最好使1个主键(自动编号)并为列添加唯一的约束?

I would vote to close your question because it is too broad. 我投票结束你的问题,因为它太广泛了。 However, you seem to be new to the site, and just closing the question wouldn't help. 但是,您似乎对这个网站是陌生的,仅关闭问题并没有帮助。

The structure of a database should be driven by how the data is going to be used. 数据库的结构应由数据的使用方式决定。 You have not specified what you are doing with the data, so it is impossible to say that one way is "better" or "worse". 您尚未指定要对数据进行的操作,因此无法说一种方式是“更好”或“更差”。 One can make a judgement that the structure is normalized or denormalized, for instance. 例如,可以判断该结构是规范化还是非规范化。 But each of those methodologies is appropriate in different circumstances. 但是,这些方法中的每一种都适用于不同的情况。

That said, I am heavily biased to having an auto incrementing ("identity") primary key in all tables. 也就是说,我非常偏向于在所有表中都具有自动递增(“标识”)主键。 This is beneficial in many circumstances. 在许多情况下这是有益的。 Here are three reasons: 这是三个原因:

  • For knowing the insertion order of rows (to a close approximation). 用于了解行的插入顺序(非常近似)。
  • For creating foreign key relationships. 用于创建外键关系。
  • To ensure that you can uniquely identify each row for updates and deletes. 为确保您可以唯一地标识更新和删除的每一行。

However, such a column occupies more storage. 但是,这样的列占用更多的存储空间。 And, a single primary key index is more efficient (space-wise) than having multiple indexes. 并且,单个主键索引比具有多个索引更有效(在空间上)。

This isn't a direct answer to your question, because the answer is too vague. 这不是您问题的直接答案,因为答案太模糊。 It does at least give you some parameters for thinking about the issues. 它至少为您提供了一些考虑问题的参数。

Usually when you don't know any better, it's best to follow the standard guidelines in DB design. 通常,当您不了解任何内容时,最好遵循数据库设计中的标准准则。 Because they work for a clear majority of the cases, and there's less chance in going wrong with them. 因为它们在大多数情况下都是有效的,而且出错的机会也很少。

In this case, it means that you would very likely benefit from using an auto-incrementing ID column as a clustered primary key index. 在这种情况下,这意味着您可能会受益于将自动递增的ID列用作群集的主键索引。 As well as adding foreign keys and nonclustered indexes to all referencing columns, in case you haven't already. 以及将外键和非聚集索引添加到所有引用列(如果您还没有的话)。

Finally, you might even look at adding a few custom (even multicolumn) indexes specifically designed for your slowest queries... basically, if you have a few problem queries, you want to index the columns in their WHERE clauses. 最后,您甚至可以考虑添加一些专为最慢的查询而设计的自定义(甚至是多列)索引...基本上,如果您有一些问题查询,则希望为其WHERE子句中的列建立索引。

In addition to all this, you should of course look to see that the table and DB in general is normalized. 除了所有这些之外,您当然应该看一下表和数据库的一般情况。 To learn about normalization, it's best for you to google for it. 要了解规范化,最好是用谷歌搜索它。 It's not a complex or difficult concept, but there are a thousand tutorials far better suited for explaining the concept than us here. 这不是一个复杂或困难的概念,但是这里有成千上万的教程比我们更适合于解释这个概念。

By doing this you will very likely be moving in the correct direction. 这样,您很可能会朝着正确的方向前进。 And if it doesn't work, then that's so much more information people here can use to determine the best alternative. 如果不起作用,那么这里的人们可以用来确定最佳替代方案的信息太多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM