我不是很熟悉数据库及其工作原理。从性能的角度(插入/更新/查询),使用字符串作主键是否比整数慢?
当前回答
默认情况下,ASPNetUserIds是128字符字符串,性能很好。
如果键必须是唯一的在表中,它应该是键。这是为什么;
主字符串键=正确的数据库关系,1个字符串键(主)和1个字符串索引(主)。
另一个选项是一个典型的int Key,但如果字符串必须是唯一的,你仍然可能需要添加一个索引,因为不停的查询来验证或检查它的唯一性。
所以使用int标识键=不正确的DB关系,1 int键(主),1 int索引(主),可能是唯一的字符串索引,手动验证相同的字符串不存在(类似sql检查可能)。
为了在主键上使用int而不是字符串获得更好的性能,当字符串必须是唯一的时,它将不得不是一个非常奇怪的情况。我总是喜欢使用字符串键。根据经验,除非需要,否则不要对数据库进行反规格化。
其他回答
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.
默认情况下,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 =…)进行有条件的查询和连接。
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,然后在自然键上放置唯一索引。您可以获得更快的连接,不会得到重复的记录,也不必因为公司名称更改而更新一百万个子记录。
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.
推荐文章
- 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
- 查询JSON类型内的数组元素
- 确定记录是否存在的最快方法
- Printf与std::字符串?
- 获得PostgreSQL数据库中当前连接数的正确查询
- 不区分大小写的“in”
- 在SQL选择语句Order By 1的目的是什么?
- MySQL数据库表中的最大记录数
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 从现有模式生成表关系图(SQL Server)
- 如何在PHP中截断字符串最接近于一定数量的字符?
- 我如何循环通过一组记录在SQL Server?
- HyperLogLog算法是如何工作的?
- 数据库和模式的区别
- Ruby数组到字符串的转换