我不是很熟悉数据库及其工作原理。从性能的角度(插入/更新/查询),使用字符串作主键是否比整数慢?
当前回答
Strings are slower in joins and in real life they are very rarely really unique (even when they are supposed to be). The only advantage is that they can reduce the number of joins if you are joining to the primary table only to get the name. However, strings are also often subject to change thus creating the problem of having to fix all related records when the company name changes or the person gets married. This can be a huge performance hit and if all tables that should be related somehow are not related (this happens more often than you think), then you might have data mismatches as well. An integer that will never change through the life of the record is a far safer choice from a data integrity standpoint as well as from a performance standpoint. Natural keys are usually not so good for maintenance of the data.
我还想指出,两者的最佳方法通常是使用自递增键(或者在某些特殊情况下,使用GUID)作为PK,然后在自然键上放置唯一索引。您可以获得更快的连接,不会得到重复的记录,也不必因为公司名称更改而更新一百万个子记录。
其他回答
从性能的角度来看-与使用整数(PK)实现的性能相比,Yes字符串(PK)将降低性能,其中PK—>主键。
From requirement standpoint - Although this is not a part of your question still I would like to mention. When we are handling huge data across different tables we generally look for the probable set of keys that can be set for a particular table. This is primarily because there are many tables and mostly each or some table would be related to the other through some relation ( a concept of Foreign Key ). Therefore we really cannot always choose an integer as a Primary Key, rather we go for a combination of 3, 4 or 5 attributes as the primary key for that tables. And those keys can be used as a foreign key when we would relate the records with some other table. This makes it useful to relate the records across different tables when required.
因此,为了优化使用-我们总是将1或2个具有1或2个字符串属性的整数组合在一起,但同样只是在需要时才这样做。
Strings are slower in joins and in real life they are very rarely really unique (even when they are supposed to be). The only advantage is that they can reduce the number of joins if you are joining to the primary table only to get the name. However, strings are also often subject to change thus creating the problem of having to fix all related records when the company name changes or the person gets married. This can be a huge performance hit and if all tables that should be related somehow are not related (this happens more often than you think), then you might have data mismatches as well. An integer that will never change through the life of the record is a far safer choice from a data integrity standpoint as well as from a performance standpoint. Natural keys are usually not so good for maintenance of the data.
我还想指出,两者的最佳方法通常是使用自递增键(或者在某些特殊情况下,使用GUID)作为PK,然后在自然键上放置唯一索引。您可以获得更快的连接,不会得到重复的记录,也不必因为公司名称更改而更新一百万个子记录。
默认情况下,ASPNetUserIds是128字符字符串,性能很好。
如果键必须是唯一的在表中,它应该是键。这是为什么;
主字符串键=正确的数据库关系,1个字符串键(主)和1个字符串索引(主)。
另一个选项是一个典型的int Key,但如果字符串必须是唯一的,你仍然可能需要添加一个索引,因为不停的查询来验证或检查它的唯一性。
所以使用int标识键=不正确的DB关系,1 int键(主),1 int索引(主),可能是唯一的字符串索引,手动验证相同的字符串不存在(类似sql检查可能)。
为了在主键上使用int而不是字符串获得更好的性能,当字符串必须是唯一的时,它将不得不是一个非常奇怪的情况。我总是喜欢使用字符串键。根据经验,除非需要,否则不要对数据库进行反规格化。
指数意味着大量的比较。
通常,字符串比整数长,并且可以应用排序规则进行比较,因此比较字符串通常比比较整数需要更多的计算量。
不过,有时使用字符串作为主键要比使用字符串与数字id表进行额外的连接更快。
使用什么作为主键并不重要,只要它是UNIQUE即可。如果您关心速度或良好的数据库设计,请使用int型,除非您计划复制数据,否则请使用GUID。
如果这是一个访问数据库或一些小应用程序,那么谁真的在乎。我认为,我们大多数开发人员之所以把旧的int或guid放在前面,是因为我们有一种方式来发展项目,并且您希望给自己留下发展的选择。