使用Guid做主键和int做主键性能比较

数据库的设计中我们常常用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

2.创建Test_Guid子表:Test_Guid_Detail和创建Test_Int子表:Test_Int_Detail,用来做连接查询

--创建Test_Guid子表:Test_Guid_Detail
USE  Test
GO

IF OBJECT_ID('Test_Guid_Detail', 'U') IS NOT NULL
  DROP TABLE Test_Guid_Detail
GO

CREATE TABLE Test_Guid_Detail
(
    Guid varchar(50) not null,--Guid是Test_Guid的外键
    TestId int not null,
    TestText ntext not null,
    TestDateTime datetime default getdate()--,
    --CONSTRAINT PK_Guid PRIMARY KEY (Guid)
)
GO
--创建Test_Int子表:Test_Int_Detail
USE  Test
GO

IF OBJECT_ID('Test_Int_Detail', 'U') IS NOT NULL
  DROP TABLE Test_Int_Detail
GO

CREATE TABLE Test_Int_Detail
(
    Id int not null,--Id是Test_Int的外键
    TestId int not null,
    TestText ntext not null,
    TestDateTime datetime default getdate()--,
    --CONSTRAINT PK_Guid PRIMARY KEY (Guid)
)
GO

3.开始测试
测试1:测试Insert:向Test_Guid表中插入10万条记录

---------------------------------------------
--测试Insert:向Test_Guid表中插入10万条记录
---------------------------------------------
declare @num int
declare @startTime datetime
set @num=0;
set @startTime=getdate()
while(@num<100000)
begin
    insert into Test_Guid
    values(newid(),@num,'测试guid',getdate())
    set @num=@num+1
end
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试2:测试Insert:向Test_Int表中插入10万条记录

---------------------------------------------
--测试Insert:向Test_Int表中插入10万条记录
---------------------------------------------
declare @num int
declare @startTime datetime
set @num=0;
set @startTime=getdate()
while(@num<100000)
begin
    insert into Test_Int
    values(@num,'测试int',getdate())
    set @num=@num+1
end
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试3:测试Select:查找Test_Guid表中所有记录

---------------------------------------------
--测试Select:查找Test_Guid表中所有记录
---------------------------------------------
declare @startTime datetime
set @startTime=getdate()
select * from Test_Guid
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试4:测试Select:查找Test_Int表中所有记录

---------------------------------------------
--测试Select:查找Test_Int表中所有记录
---------------------------------------------
declare @startTime datetime
set @startTime=getdate()
select * from Test_Int
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试5:聚合查询:查找Test_Guid表中所有记录数

---------------------------------------------
--聚合查询:查找Test_Guid表中所有记录
---------------------------------------------
declare @startTime datetime
set @startTime=getdate()
select count(*) from Test_Guid
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试6:聚合查询:查找Test_Int表中所有记录数

---------------------------------------------
--聚合查询:查找Test_Int表中所有记录
---------------------------------------------
declare @startTime datetime
set @startTime=getdate()
select count(*) from Test_Int
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试7:测试带where条件的Select查询:查找Test_Int表中所有记录,都查找10000到50000之间的4万条记录

----------------------------------------------------------------------------------------
--测试带where条件的Select查询:查找Test_Int表中所有记录,都查找10000到50000之间的4万条记录
----------------------------------------------------------------------------------------
declare @startTime datetime
set @startTime=getdate()
select * from Test_Guid where TestId between 10000 and 50000 
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试8:测试带where条件的Select查询:查找Test_Int表中所有记录,都查找10000到50000之间的4万条记录

----------------------------------------------------------------------------------------
--测试带where条件的Select查询:查找Test_Int表中所有记录,都查找10000到50000之间的4万条记录
----------------------------------------------------------------------------------------
declare @startTime datetime
set @startTime=getdate()
select * from Test_Int where TestId between 10000 and 50000 
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试9:测试Test_Guid关联查询inner join

首先以Test_Guid中第一个Guid为外键,向Test_Guid_Detail中插入1万条记录

---------------------------------------------
--向Test_Guid子表:Test_Guid_Detail中插入1万条记录
---------------------------------------------
declare @num int
declare @topGuid nvarchar(50)
set @num=0;
select top 1 @topGuid=Guid from Test_Guid
while(@num<10000)
begin
    insert into Test_Guid_Detail
    values(@topGuid,@num,'测试guid的子表',getdate())
    set @num=@num+1
end

然后开始测试:

---------------------------------------------
--测试连接查询:查找Test_Guid表和Test_Guid_Detail所有关联的记录
---------------------------------------------
declare @startTime datetime
set @startTime=getdate()
select T.* from Test_Guid T
inner join Test_Guid_Detail T1 on T.Guid=T1.Guid
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试10:测试Test_Int关联查询inner join

首先以Test_Int中第一个id为外键,向Test_Int_Detail中插入1万条记录

---------------------------------------------
--向Test_Int子表:Test_Int中插入1万条记录
---------------------------------------------
declare @num int
declare @topInt int
set @num=0;
select top 1 @topInt=Id from Test_Int
while(@num<10000)
begin
    insert into Test_Int_Detail
    values(@topInt,@num,'测试int的子表',getdate())
    set @num=@num+1
end

然后开始测试:

---------------------------------------------
--测试连接查询:查找Test_Int表和Test_Int_Detail所有关联的记录
---------------------------------------------
declare @startTime datetime
set @startTime=getdate()
select T.* from Test_Int T
inner join Test_Int_Detail T1 on T.id=T1.id
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试11:测试Update:更新Test_Guid表中所有记录

---------------------------------------------
--测试Update:查找Test_Guid表中所有记录
---------------------------------------------
declare @startTime datetime
set @startTime=getdate()
update  Test_Guid set TestText='测试guid更新'
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试12:测试Update:更新Test_Int表中所有记录

---------------------------------------------
--测试Update:查找Test_Int表中所有记录
---------------------------------------------
declare @startTime datetime
set @startTime=getdate()
update Test_Int set TestText='测试int更新'
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒

测试13:测试Delete:删除Test_Guid表中所有记录

---------------------------------------------
--测试Delete:查找Test_Guid表中所有记录
---------------------------------------------
declare @startTime datetime
set @startTime=getdate()
delete from Test_Guid
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒
delete from Test_Guid_Detail

测试14:测试Delete:删除Test_Int表中所有记录

---------------------------------------------
--测试Delete:查找Test_Int表中所有记录
---------------------------------------------
declare @startTime datetime
set @startTime=getdate()
delete from Test_Int
select datediff(second,@startTime,getdate()) as 秒,datediff(ms,@startTime,getdate()) as 毫秒
delete from Test_int_Detail

测试结果如下:

上所述,使用int做主键比用guid做主键各中情况下效率均有提高,特别是在有连接查询和删除记录效率提升明显。

而且本人今日在guid做主键的数据查询中因为嵌套几个子查询结果屡屡出现查询超时。因此本人赞同用int做主键,不赞同guid做主键。
以上观点代表个人观点,欢迎大家各抒己见,说明guid和int各自做主键的优劣所在。

附上测试脚本供大家测试:http://files.cnblogs.com/jackhuclan/guid.rar

后续测试:
 经过各位兄弟的提醒,今天在两个子表添加了非聚集索引:

CREATE NONCLUSTERED INDEX Index_Detail_Guid on Test_Guid_Detail(Guid) 
CREATE NONCLUSTERED INDEX Index_Detail_id on Test_Int_Detail(id) 
  然后进行内连接查询,发现如@徐少侠说所的,效率确实不至于提示50%以上明显,基本只有23%左右的提升,这个还是可以接受的。
因此建议
1.在经常需要做数据迁移的系统中,建议用Guid。并且在相应的外键字段,也就是用来做连接查询的字段添加非聚集索引,对于改善性能有极大的好处。where条件的字段也可以适当添加非聚集索引。
2.在使用Guid类型作为主键时,数据类型应为uniqueidentifier,并且一定要记得取消主键的“聚集索引”
3.对于不需要做迁移,或小型系统,用int做主键还是很方便的,并且在效率方面还是有一定提升的。

使用Guid做主键和int做主键性能比较的更多相关文章

  1. mysql5.5 uuid做主键与int做主键的性能实测

    数据库:mysql5.5 表类型:InnoDB 数据量:100W条 第一种情况: 主键采用uuid 32位. 运行查询语句1:SELECT COUNT(id) FROM test_varchar; 运 ...

  2. SQL GUID和自增列做主键的优缺点

    我们公司的数据库全部是使用GUID做主键的,很多人习惯使用int做主键.所以呢,这里总结一下,将两种数据类型做主键进行一个比较. 使用INT做主键的优点: 1.需要很小的数据存储空间,仅仅需要4 by ...

  3. 扩展ASP.NET Identity使用Int做主键

    当我们默认新建一个ASP.NET MVC项目的时候,使用的身份认证系统是ASP.NET Identity.但是这里的Identity使用的主键为String类型的GUID.当然这是大多数系统首先类型. ...

  4. MySQL数据库--外键约束及外键使用

    什么是主键.外键关系型数据库中的一条记录中有若干个属性,若其中某一个属性组(注意是组)能唯一标识一条记录,该属性组就可以成为一个主键. 比如: 学生表(学号,姓名,性别,班级) 其中每个学生的学号是唯 ...

  5. SQL学习:主键,外键,主键表,外键表,数据库的表与表之间的关系;

    在数据库的学习中,对于一个表的主键和外键的认识是非常重要的. 主键:在一个表中,能唯一的表示一个事物(或者一条记录)的字段,我们称之为主键 注意: 主键的设置可以不只是用一个字段,也可以用若干个字段的 ...

  6. 发送WIN+SAPCE键,WINDOWS,空格键

    键盘代码部份转自:http://www.cnblogs.com/cpcpc/archive/2011/02/22/2123055.html 由于喜欢用CTRL+空格键切换输入法,在WIN8上有所不习惯 ...

  7. MSSQL - 逻辑主键、业务主键和复合主键

    转载自:http://blog.csdn.net/sunrise918/article/details/5575054 这几天对逻辑主键.业务主键和复合主键进行了一些思考,也在网上搜索了一下相关的讨论 ...

  8. MySQL基础9-主键约束、外键约束、等值连接查询、一对一和多对多关系

    1.主键约束和外键约束 外键约束 * 外键必须是另一表的主键的值(外键要引用主键!) * 外键可以重复 * 外键可以为空 * 一张表中可以有多个外键! 概念模型在数据库中成为表 数据库表中的多对一关系 ...

  9. Redis源码解析:09redis数据库实现(键值对操作、键超时功能、键空间通知)

    本章对Redis服务器的数据库实现进行介绍,说明Redis数据库相关操作的实现,包括数据库中键值对的添加.删除.查看.更新等操作的实现:客户端切换数据库的实现:键超时相关功能的实现.键空间事件通知等. ...

随机推荐

  1. jQuery的DOM操作实例(1)——选项卡&&Tab切换

    一.原生JavaScript编写tab切换 二.jQuery编写tab切换 在用jQuery编写选项卡过程中,重要的事搞清楚 .eq() 和 .index() 的使用方法. .eq()是jQuery遍 ...

  2. markdown常用语法总结

    转自markdown示例[模板] 1.1.段落标题 根据原文中的文档标题可以对应设置标题. # 一级标题## 二级标题### 三级标题 效果 => 一级标题 二级标题 三级标题 1.2.斜体.加 ...

  3. 微信小程序的应用及信息整合,都放到这里了

    微信小程序终于开始公测了,这篇文章也终于可以发布了. 这篇文章可以说是微信小程序系列三部曲最后一篇.8 月份,小程序推出前,我写了<别开发 app 了>详细阐述了为什么创业应该放弃原生 a ...

  4. 最适合作为Java基础面试题之Singleton模式

    看似只是最简单的一种设计模式,可细细挖掘,static.synchronized.volatile关键字.内部类.对象克隆.序列化.枚举类型.反射和类加载机制等基础却又不易理解透彻的Java知识纷纷呼 ...

  5. 数据库进阶之路(五) - MySQL行锁深入研究

    由于业务逻辑的需要,必须对数据表的一行或多行加入行锁,举个最简单的例子,图书借阅系统:假设id=1的这本书库存为1,但是有2个人同时来借这本书,此处的逻辑为: ; --如果restnum大于0,执行u ...

  6. Xamarin for Visual Studio V3.11.431 于 2015.4.3-2015.4.17 最新发布(Win & Mac)

    Beta Release: April 3 edited April 17 in Visual Studio Released versions: Windows Xamarin.VisualStud ...

  7. Android Service

    一.在MainAcitivity界面启动Service  : public class MyService extends Service intent = new Intent(MainActivi ...

  8. PHP学习笔记:输入一句话,实现单词倒序输出

    约定:句子以空格为词语分割符号,以句号为结束符号. 实现思路: 用函数explode(separator,string,limit)对字符串进行分割,再对得到的数据最后一个成员分割切掉符号.用一个新的 ...

  9. jQuery的$.ajax

    在介绍JSONP之前,先简单的介绍一些JSON.JSON是JavaScript Object Notation的缩写,是一种轻量的.可读的基于文本的数据交换开放标准.源于JavsScript编程语言中 ...

  10. activiti工作流的web流程设计器整合视频教程 SSM 和 独立部署

    本视频为activiti工作流的web流程设计器整合视频教程 整合Acitiviti在线流程设计器(Activiti-Modeler 5.21.0 官方流程设计器) 本视频共讲了两种整合方式 1. 流 ...