我们的数据位于 SQL Server 2008 数据库中,表之间会有很多查询和连接.我们团队内部有这个论点,有些人认为使用整数标识对性能更好,有些人争论使用 guid(唯一标识符).
Our data resides in a SQL Server 2008 database, there will be a lot queries and joinings between tables. We have this argument inside the team, some are arguing use of integer identity is better for performance, some are arguing use of guid (unique identifier).
使用 GUID 作为主键的性能真的会受到如此严重的影响吗?
Does the performance really suffer that badly using a GUID as a primary key?
128 位 GUID (uniqueidentifier
) 键当然比 32 位 int
键.但是,有一些关键优势:
A 128-bit GUID (uniqueidentifier
) key is of course 4x larger than a 32-bit int
key. However, there are a few key advantages:
CAST()
调用,您甚至可以根据日期/时间范围从主键中 SELECT
.SELECT scope_identity()
在插入后获取主键的步骤.bigint
(64 位)而不是 int
.一旦你这样做了,uniqueidentifier
只是 bigint
的两倍.SELECT
from the primary key based on a date/time range if you want with a few fancy CAST()
calls.SELECT scope_identity()
to get the primary key after an insert.bigint
(64 bits) instead of int
. Once you do that, uniqueidentifier
is only twice as big as a bigint
.最后,通过使用整数来挤出一些小的性能优势可能不值得失去 GUID 的优势.凭经验对其进行测试并自行决定.
In the end, squeezing out some small performance advantage by using integers may not be worth losing the advantages of a GUID. Test it empirically and decide for yourself.
就我个人而言,我仍然根据情况使用两者,但在我的情况下,决定性因素从未真正归结为性能.
Personally, I still use both, depending on the situation, but the deciding factor has never really come down to performance in my case.
这篇关于唯一标识符(guid)作为数据库设计中的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!