在数据库表设计时,许多人为采用INT类型还是GUID(uniqueidentifyer)作为主键争论不休,有认为int型字段好的,有认为GUID好的,很多时候的焦点集中在效率上。
为了弄清事实真相,我想还是要以实验来进行测试为准。以下就是为了测试插入效率而写的一段脚本。测试环境是:Xeon 1.6/2G内存 win2003/sqlserver2005 企业版。
测试脚本:
--测试无主键/Identity/Uniqueidentifier/varchar类型主键插入表时的效率 set nocount on declare @now datetime,@i int set @i=1 set @now=getdate() Create table test1(nopkey int,col uniqueidentifier ) while @i<10000000 Begin insert into test1 values (1,newid()) set @i=@i+1 end Print'新表无主键插入100万条数据所耗时间:'+convert(varchar(200),datediff(ms,@now,Getdate()))+'毫秒' set @i=1 set @now=getdate() while @i<10000 Begin insert into test1 values (1,newid()) set @i=@i+1 end Print'100万行中再插入10000条数据时间:'+convert(varchar(200),datediff(ms,@now,Getdate()))+'毫秒' GO Create table Test2(pkey int identity(1

在数据库设计中,INT和GUID作为主键的选择常引发争议。本文通过在Sql2005环境下进行的插入效率测试,探讨两种类型的主键在性能上的差异。测试在Xeon 1.6GHz/2GB内存的win2003服务器上进行,以实证方式展示结果。
最低0.47元/天 解锁文章
650

被折叠的 条评论
为什么被折叠?



