我不是很熟悉数据库及其工作原理。从性能的角度(插入/更新/查询),使用字符串作主键是否比整数慢?


Technically yes, but if a string makes sense to be the primary key then you should probably use it. This all depends on the size of the table you're making it for and the length of the string that is going to be the primary key (longer strings == harder to compare). I wouldn't necessarily use a string for a table that has millions of rows, but the amount of performance slowdown you'll get by using a string on smaller tables will be minuscule to the headaches that you can have by having an integer that doesn't mean anything in relation to the data.


指数意味着大量的比较。

通常,字符串比整数长,并且可以应用排序规则进行比较,因此比较字符串通常比比较整数需要更多的计算量。

不过,有时使用字符串作为主键要比使用字符串与数字id表进行额外的连接更快。


变量太多了。这取决于表的大小,索引,字符串键域的性质…

一般来说,整数会更快。但差别大到足以让人在意吗?这很难说。

另外,你选择字符串的动机是什么?数字型的自动递增键通常也容易得多。是语义上的吗?方便?复制/断开连接问题?你的回答可能会限制你的选择。这也会让你想起你忘记的第三个“混合”选项:Guids。


使用string作为主键的另一个问题是,由于索引不断按顺序排列,当创建一个新键时,索引必须重新排序……如果使用自动编号整数,则新键只添加到索引的末尾。


是的,但除非您希望有数百万行,否则不使用基于字符串的键(因为它较慢)通常是“过早优化”。毕竟,字符串存储为大数字,而数字键通常存储为较小的数字。

不过,要注意的一件事是,如果您在任意键上聚集了索引,并且在索引中进行了大量的非顺序插入。写入的每一行都将导致索引重新写入。如果您正在进行批量插入,这确实会降低过程的速度。


你为什么要用字符串作为主键?

我只需将主键设置为一个自动递增的整数字段,并在字符串字段上放置一个索引。

这样,如果您在表上进行搜索,它们应该相对较快,并且所有的连接和正常查找都不会受到速度的影响。

您还可以控制被索引的字符串字段的数量。换句话说,如果您认为这样就足够了,您可以说“只索引前5个字符”。或者如果您的数据可以相对相似,您可以索引整个字段。


使用什么作为主键并不重要,只要它是UNIQUE即可。如果您关心速度或良好的数据库设计,请使用int型,除非您计划复制数据,否则请使用GUID。

如果这是一个访问数据库或一些小应用程序,那么谁真的在乎。我认为,我们大多数开发人员之所以把旧的int或guid放在前面,是因为我们有一种方式来发展项目,并且您希望给自己留下发展的选择。


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,然后在自然键上放置唯一索引。您可以获得更快的连接,不会得到重复的记录,也不必因为公司名称更改而更新一百万个子记录。


有可能是一个非常大的误解有关字符串在数据库中。几乎每个人都认为数字的数据库表示比字符串更紧凑。他们认为db-s中的数字表示为内存中的数字。但事实并非如此。在大多数情况下,数字表示法更接近于字符串表示法。

使用数字或字符串的速度更依赖于索引,而不是类型本身。


从性能的角度来看-与使用整数(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个字符串属性的整数组合在一起,但同样只是在需要时才这样做。


不要担心性能,直到您获得了一个简单而合理的设计,该设计与数据描述的主题一致,并且非常适合数据的预期用途。然后,如果出现性能问题,您可以通过调整系统来处理它们。

在这种情况下,使用字符串作为自然的主键几乎总是更好的,只要您可以信任它。如果是字符串也不用担心,只要字符串足够短,比如说最多25个字符。就性能而言,你不会付出很大的代价。

数据输入人员或自动数据源是否总是为假定的自然键提供一个值,还是有时会省略?输入数据偶尔会出错吗?如果是,如何检测和纠正错误?

指定查询的程序员和交互用户能够使用自然键来获得他们想要的东西吗?

如果你不相信天然的钥匙,那就找一个替代品。如果你发明了一个代理,你也可以发明一个整数。然后,您必须考虑是否对用户社区隐藏代理。一些没有隐藏代理键的开发人员后来后悔了。


Inserts to a table having a clustered index where the insertion occurs in the middle of the sequence DOES NOT cause the index to be rewritten. It does not cause the pages comprising the data to be rewritten. If there is room on the page where the row will go, then it is placed in that page. The single page will be reformatted to place the row in the right place in the page. When the page is full, a page split will happen, with half of the rows on the page going to one page, and half going on the other. The pages are then relinked into the linked list of pages that comprise a tables data that has the clustered index. At most, you will end up writing 2 pages of database.


在PK列中使用整数有两个原因:

我们可以为自动递增的整数字段设置标识。 当我们创建pk时,db会创建一个索引(Cluster或Non Cluster),在数据存储到表之前对其进行排序。通过在PK上使用标识,优化器在保存记录之前不需要检查排序顺序。这提高了大表的性能。


默认情况下,ASPNetUserIds是128字符字符串,性能很好。

如果键必须是唯一的在表中,它应该是键。这是为什么;

主字符串键=正确的数据库关系,1个字符串键(主)和1个字符串索引(主)。

另一个选项是一个典型的int Key,但如果字符串必须是唯一的,你仍然可能需要添加一个索引,因为不停的查询来验证或检查它的唯一性。

所以使用int标识键=不正确的DB关系,1 int键(主),1 int索引(主),可能是唯一的字符串索引,手动验证相同的字符串不存在(类似sql检查可能)。

为了在主键上使用int而不是字符串获得更好的性能,当字符串必须是唯一的时,它将不得不是一个非常奇怪的情况。我总是喜欢使用字符串键。根据经验,除非需要,否则不要对数据库进行反规格化。


我可能会使用一个整数作为你的主键,然后把你的字符串(我假设它是某种ID)作为一个单独的列。

create table sample (
  sample_pk             INT NOT NULL AUTO_INCREMENT,
  sample_id             VARCHAR(100) NOT NULL,
  ...
  PRIMARY KEY(sample_pk)
);

您总是可以对字符串(ID)列(其中sample_id =…)进行有条件的查询和连接。