SQL 主键,INT 或 GUID 或..?
我是否有任何理由不应该使用整数作为表的主键?
数据库是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该使用整数 - 它更小,意味着更少的内存、更少的 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.
使用
GUID
primkey的好处是它应该是全球唯一的,例如是否将数据从一个数据库移动到另一个数据库。所以您知道该行是唯一的。但如果我们谈论的是小数据库,那么我更喜欢整数。
编辑:
如果您使用 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.
我认为在这种情况下没有理由不使用自动递增整数。如果您遇到整数无法处理数据量的情况,那么您正在谈论的应用程序已扩展到需要进行更多工作的程度。
请记住以下几点:
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:
不,只要每一个都是唯一的,整数就可以了。指南一开始听起来是个好主意,但实际上它们太大了。大多数时候,它使用大锤来杀死苍蝇,而 Guid 的大小使其比使用整数慢得多。
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.
一定要使用整数,您不想在聚集索引 (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.