字符串作为 SQL 数据库中的主键 [英] Strings as Primary Keys in SQL Database

查看:40
本文介绍了字符串作为 SQL 数据库中的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am not very familiar with databases and the theories behind how they work. Is it any slower from a performance standpoint (inserting/updating/querying) to use Strings for Primary Keys than integers?

推荐答案

技术上是的,但如果一个字符串可以作为主键,那么你可能应该使用它.这一切都取决于您为其制作的表的大小以及将成为主键的字符串的长度(更长的字符串 == 更难比较).对于具有数百万行的表,我不一定会使用字符串,但是在较小的表上使用字符串所导致的性能下降量对于使用整数所带来的麻烦来说是微不足道的与数据无关.

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.

这篇关于字符串作为 SQL 数据库中的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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