Welcome

首页 / 数据库 / SQLServer / 使用Guid做主键和int做主键性能比较

使用Guid做主键和int做主键性能比较2011-09-18 博客园 Jackhuclan在数据库的设计中我们常常用Guid或int来做主键,根据所学的知识一直感觉int做主键效率要高,但 没有做仔细的测试无法

说明道理。碰巧今天在数据库的优化过程中,遇到此问题,于是做了一下测试。

测试环境:

台式电脑 Pentiun(R) 4 Cpu 3.06GHz

Win XP professional

1.5G DDR RAM

SQL Server 2005 个人版

测试过程:

首先创建测试数据库Test

1.创建Test_Guid表,创建Test_Int表

代码

-------------------------------------------
--创建Test_Guid表
---------------------------------------------
USE Test
GO
IF OBJECT_ID("Test_Guid", "U") IS NOT NULL
DROP TABLE Test_Guid
GO
CREATE TABLE Test_Guid

Guid varchar(50) not null,
TestId int not null,
TestText ntext not null,
TestDateTime datetime default getdate(),
CONSTRAINT PK_Guid PRIMARY KEY (Guid)

GO
---------------------------------------------
--创建Test_Int表
---------------------------------------------
USE Test
GO
IF OBJECT_ID("Test_Int", "U") IS NOT NULL
DROP TABLE Test_Int
GO
CREATE TABLE Test_Int

Id int not null identity(1,1),
TestId int not null,
TestText ntext not null,
TestDateTime datetime default getdate(),
CONSTRAINT PK_Id PRIMARY KEY (Id)

GO