博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于数据库字段长度对于查询性能的小测试

Posted on 2012-06-04 09:15  hyd309  阅读(3420)  评论(0编辑  收藏  举报

 

 

近期,公司对数据库进行一系列的优化,优化过程中,产生了数据库字段长度对于查询时间是否有影响的问题争论,因此,做如下实验:

创建两张表,table1,table2

分别创建字段 waternum nvchar(4000),waternum nvchar(50)

插入数据,对两张表插入250000条数据

--增加数据到第一张表

declare @a datetime

declare @i int

set @a=getdate()

--这里加试验语句

declare @b datetime

declare @s int

set @s=1

while @s<=250000

begin

    set @b=getdate()

    insert into table1 values(@b)

    set @s=@s+1

end

select count(*) as tb1 from table1

--

set @i=datediff(millisecond,@a,getdate()) --millisecond表示毫秒

print @i

go

--增加数据到第二张表

declare @a datetime

declare @i int

set @a=getdate()

--这里加试验语句

declare @b datetime

declare @s int

set @s=1

while @s<=250000

begin

    set @b=getdate()

    insert into table2 values(@b)

    set @s=@s+1

end

 

select count(*) as tb2 from table2

--

set @i=datediff(millisecond,@a,getdate()) --millisecond表示毫秒

print @i

go

第一张表,数据插入耗时:95830毫秒 81263   59970

第二张表,数据插入耗时:67030毫秒 135983  58220

 

--查询第一张表

declare @a datetime

declare @i int

set @a=getdate()

--这里加试验语句

--print 'jkjk'

select waternum from table1

--

set @i=datediff(millisecond,@a,getdate()) --millisecond表示毫秒

print @i

go

--查询第二张表

declare @a datetime

declare @i int

set @a=getdate()

--这里加试验语句

--print 'jkjk'

select waternum from table1

--

set @i=datediff(millisecond,@a,getdate()) --millisecond表示毫秒

print @i

go

 

第一张表查询waternum耗时: 923毫秒 830  920

第二张表查询waternum耗时: 860毫秒 860  813

 

清除数据时间

 

--删除第一张表数据

declare @a datetime

declare @i int

set @a=getdate()

--这里加试验语句

delete from table1

--

set @i=datediff(millisecond,@a,getdate()) --millisecond表示毫秒

print @i

go

--删除第二张表数据

declare @a2 datetime

declare @i2 int

set @a2=getdate()

--这里加试验语句

delete from table2

--

set @i2=datediff(millisecond,@a2,getdate()) --millisecond表示毫秒

print @i2

go

 

第一张表耗时:670毫秒  643  576

第二张表耗时:1046毫秒 626  576

 

总上三次统计,差距结果并不明显,故将两张表字段全部设置为waternum nvchar(50)进程一次测试

插入数据:

第一张表:65046

第二张表:58406

查询数据:

第一张表:1093

第二张表:843

删除数据:
第一张表:563

第二张表:720

 

综上测试,数据库字段长度并无影响数据库的查询性能,存在数值大小属于sql查询正常的时间浮动。