本文介绍了SQL Server 中是否需要主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常幼稚和愚蠢的问题,但我还是会问这个问题

This may be a pretty naive and stupid question, but I'm going to ask it anyway

我有一个包含多个字段的表,其中没有一个是唯一的,还有一个主键,显然是.

I have a table with several fields, none of which are unique, and a primary key, which obviously is.

此表定期通过非唯一字段访问,但没有用户 SP 或进程通过主键访问数据.那么主键是必需的吗?是否在幕后使用?删除它会积极还是消极地影响性能?

This table is accessed via the non-unique fields regularly, but no user SP or process access data via the primary key. Is the primary key necessary then? Is it used behind the scenes? Will removing it affect performance Positively or Negatively?

推荐答案

有必要吗?不.在幕后使用?好吧,它已保存到磁盘并保存在行缓存中等等.删除会稍微提高您的性能(使用毫秒精度的手表来注意).

Necessary? No. Used behind the scenes? Well, it's saved to disk and kept in the row cache, etc. Removing will slightly increase your performance (use a watch with millisecond precision to notice).

但是......下次有人需要创建对这个表的引用时,他们会诅咒你.如果他们勇敢,他们会添加一个PK(并等待很长时间让DB创建列).如果他们不勇敢或不笨,他们将开始使用业务键(即数据列)创建引用,这将导致维护噩梦.

But ... the next time someone needs to create references to this table, they will curse you. If they are brave, they will add a PK (and wait for a long time for the DB to create the column). If they are not brave or dumb, they will start creating references using the business key (i.e. the data columns) which will cause a maintenance nightmare.

结论:既然拥有 PK(即使不使用 ATM)的成本如此之低,就这样吧.

Conclusion: Since the cost of having a PK (even if it's not used ATM) is so small, let it be.

这篇关于SQL Server 中是否需要主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:54