5

与使用基于字符的字段相比,在数据库表中使用唯一的数字 ID 字段是否有性能提升或最佳实践?

例如,如果我有两个表:

运动员

id ... 17 ,姓名... Rickey Henderson , teamid ... 28

团队

teamid ... 28 , teamname ... Oakland

如果teamid是“OAK”或“SD”而不是“28”或“31” ,那么包含数千名玩家的运动员表将更易于阅读。让我们理所当然地认为teamid值将在角色形式上保持独特和一致。

我知道您可以使用字符,但是出于某种原因,索引、过滤等是否是个坏主意?

请忽略规范化参数,因为这些表比示例更复杂。

4

7 回答 7

16

我发现从长远来看,无意义数字的主键会减少头痛。

于 2009-01-15T04:38:54.547 回答
4

由于您提到的所有原因,文字很好。

如果字符串只有几个字符,那么无论如何它几乎都是一个整数。使用字符串的最大潜在缺点是大小:数据库性能与需要多少磁盘访问有关。例如,将索引设置为两倍大,可能会产生磁盘缓存压力,并增加磁盘寻道次数。

于 2009-01-15T04:39:34.607 回答
3

我会远离使用文本作为您的密钥 - 将来当您想更改某个团队的团队 ID 时会发生什么?您必须在整个数据中级联该键更改,而这正是主键可以避免的事情。此外,虽然我没有任何经验证据,但我认为 INT 键会比文本键快得多。

也许您可以为您的数据创建更易于使用的视图,同时仍使用数字主键。

于 2009-01-15T04:39:44.433 回答
3

I'm just going to roll with your example. Doug is correct when he says that text is fine. Even for a medium sized (~50gig) database having a 3 letter code be a primary key won't kill the database. If it makes development easier, reduces joins on the other table and it's a field that users would be typing in...I say go for it. Don't do it if it's just an abbreviation that you show on a page or because it makes the athletes table look pretty. I think the key is the question "Is this a code that the user will type in and not just pick from a list?"

Let me give you an example of when I used a text column for a key. I was making software for processing medical claims. After the claim got all digitized a human had to look at the claim and then pick a code for it that designated what kind of claim it was. There were hundreds of codes...and these guys had them all memorized or crib sheets to help them. They'd been using these same codes for years. Using a 3 letter key let them just fly through the claims processing.

于 2009-01-15T05:03:27.587 回答
2

我建议使用 ints 或 bigints 作为主键。好处包括:

  • 这允许更快的连接。
  • 主键中没有语义含义允许您更改具有语义含义的字段,而不会影响与其他表的关系。

您总是可以有另一列来保存 team_code 或“OAK”和“SD”的东西。还

于 2009-01-15T04:39:59.287 回答
2

标准答案是使用数字,因为它们的索引速度更快;无需计算哈希或其他任何东西。

如果您使用有意义的值作为主键,则如果团队名称发生更改,则必须通过您的数据库对其进行全部更新。

满足以上,但仍使数据库直接可读,

  • 使用数字字段作为主键

  • 立即创建连接 Athlete 和 Team 表的 Athlete_And_Team 视图

Then you can use the view when you're going through the data by hand.

于 2009-01-15T04:47:08.883 回答
0

Are you talking about your primary key or your clustered index? Your clustered index should be the column which you will use to uniquely identify that row by most often. It also defines the logical ordering of the rows in your table. The clustered index will almost always be your primary key, but there are circumstances where they can be differant.

于 2009-01-15T04:48:18.833 回答