SQL 主键,INT 或 GUID 或..?

发布于 2024-10-09 11:57:16 字数 255 浏览 6 评论 0原文

我是否有任何理由不应该使用整数作为表的主键?

数据库是 SQL-CE,有两个每年大约 50,000 个条目的主表,以及一些次要表。只有两个连接会存在,对数据库持续打开。但是更新将通过多个 TCP 套接字连接触发,因此将有许多跨线程访问和使用同一个数据库连接。虽然活动非常低,所以同时更新的可能性很小,但最多可能每天发生几次。

可能会使用 LINQ2SQL 来处理 DAL 或类型化数据集。

不确定此信息是否相关,但这就是我问的原因,因为我不知道:)

Is there any reason why I should not use an Integer as primary key for my tables?

Database is SQL-CE, two main tables of approx 50,000 entries per year, and a few minor tables. Only two connections will exist constantly open to the database. But updates will be triggered through multiple TCP socket connections, so it will be many cross threads that access and use the same database-connection. Although activity is very low, so simultanous updates are quite unlikely, but may occur maybe a couple of times per day max.

Will probably use LINQ2SQL for DAL, or typed datasets.

Not sure if this info is relevant, but that's why I'm asking, since I don't know :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

つ可否回来 2024-10-16 11:57:16

应该使用整数 - 它更小,意味着更少的内存、更少的 IO(磁盘和网络)、更少的连接工作。

无论 PK 的类型如何,数据库都应该处理并发问题。

You should use an integer - it is smaller, meaning less memory, less IO (disk and network), less work to join on.

The database should handle the concurrency issues, regardless of the type of PK.

回眸一笑 2024-10-16 11:57:16

使用GUIDprimkey的好处是它应该是全球唯一的,例如是否将数据从一个数据库移动到另一个数据库。所以您知道该行是唯一的。

但如果我们谈论的是小数据库,那么我更喜欢整数。

编辑:

如果您使用 SQL Server 2005++,您还可以使用 NEWSEQUENTIALID(),
这会根据上面的行生成一个 GUID。允许 newid() 的索引问题不再存在。

The advantage of using GUID primkey is that it should be unique in the world, such as whether to move data from one database to another. So you know that the row is unique.

But if we are talking about a small db, so I prefer integer.

Edit:

If you using SQL Server 2005++, can you also use NEWSEQUENTIALID(),
this generates a GUID based on the row above.Allows the index problem with newid() is not there anymore.

谈下烟灰 2024-10-16 11:57:16

我认为在这种情况下没有理由不使用自动递增整数。如果您遇到整数无法处理数据量的情况,那么您正在谈论的应用程序已扩展到需要进行更多工作的程度。

请记住以下几点:

  1. 整数是硬件的本机字大小。它在计算机上与数据类型一样快速、简单和容易。
  2. 当考虑可能使用 GUID 时,要知道它们会导致糟糕的主键。一般来说,关系数据库(我不能代表所有,但 MS SQL 是一个很好的例子)不能很好地索引 GUID。有一些黑客试图制作更多索引友好的 GUID,要么采用它们,要么放弃它们。但一般来说,出于性能原因,应避免将 GUID 作为 PK。

I see no reason not to use an auto-increment integer in this scenario. If you ever get to the point where an integer can't handle the volume of data then you're talking about an application scaled up to the point that a lot more work is involved anyway.

Keep in mind a few things:

  1. An integer is the native word size for the hardware. It's about as fast and simple and easy on the computer as a data type gets.
  2. When considering possibly using a GUID, know that they make for terrible primary keys. Relational databases in general (I can't speak for all, but MS SQL is a good example) don't index GUIDs well. There are hacks out there to try to make more index-friendly GUIDs, take them or leave them. But in general a GUID should be avoided as a PK for performance reasons.
伪装你 2024-10-16 11:57:16

有什么理由我不应该这样做吗?
使用整数作为我的主键
表?

不,只要每一个都是唯一的,整数就可以了。指南一开始听起来是个好主意,但实际上它们太大了。大多数时候,它使用大锤来杀死苍蝇,而 Guid 的大小使其比使用整数慢得多。

Is there any reason why I should not
use an Integer as primary key for my
tables?

Nope, as long as each one is unique, integers are fine. Guids sounds like a good idea at first, but in reality they are much too large. Most of the time, it's using a sledgehammer to kill a fly, and the size of the Guid makes it much slower than using an integer.

铁轨上的流浪者 2024-10-16 11:57:16

一定要使用整数,您不想在聚集索引 (PK) 中使用 GUID,因为它会导致表产生不必要的碎片。

Definitely use an integer, you do not want to use a GUID in a clustered index (PK) as it will cause the table to unnecessarily fragment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文