热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> 数据库>>正文内容

SQL Server 2005大批量数据操作使用实例

发布时间:2008年12月09日点击数: 未知
sql server2005数据库中建立两个类型相同的数据表,如下:
表一:
create table test1  
(  
    iId        int identity(1,1) not null,  
    vTest1Code varchar(30)          not null,  
    vName      varchar(30)          not null,  
    dDate      datetime,  
    primary key(iId)  
)
表二:
create table test2
(
    Id     int identity(1,1) not null,
    Code   varchar(30)          not null,
    Name   varchar(30)          not null,
    date   datetime,
    primary key(Id)
)

两表所占用的系统空间

exec sp_spaceused 'test1'
go
exec sp_spaceused 'test2'

Name

Rows

Reserved

Data

Index_size

unused

 

Test1

0

0KB

0KB

0KB

0KB

 

Test2

0

0KB

0KB

0KB

0KB

 

               

由上图得知两表所占用的系统空间一致。

         执行数据插入操作

 

--测试TEST1
declare @startTime datetime
set @startTime=getdate()
declare @i int
set @i=1
while @i<100
begin
insert into test1(vTest1Code,vName) values('vCode'+CAST(@i as varchar(10)),'vName'+CAST(@i as varchar(10)))
set @i=@i+1
end
select [语句执行花费时间(毫秒)]=datediff(ms,@startTime,getdate())
go

 

--测试TEST2
declare @startTime datetime
set @startTime=getdate()
declare @i int
set @i=1
while @i<100
begin
insert into test2(Code,Name) values('vCode'+CAST(@i as varchar(10)),'vName'+CAST(@i as varchar(10)))
set @i=@i+1
end
select [语句执行花费时间(毫秒)]=datediff(ms,@startTime,getdate())
go

插入耗时情况

 

test1语句执行花费时间(毫秒)

test2语句执行花费时间(毫秒)

100

30

30

1000

250

250

10000

2623

2516

100000

26453

26560

1000000

275110

282796

最后两表所占用的系统空间

exec sp_spaceused 'test1'

exec sp_spaceused 'test2'

Name

Rows

Reserved

Data

Index_size

unused

 

Test1

1000098

48520KB

48272KB

192KB

56KB

 

Test2

1000098

48520KB

48272KB

192KB

56KB

 

               

问题现象描述:

<!--[if !supportLists]-->1、  <!--[endif]-->在相同数据类型、长度,及约束、索引的情况下,执行千条及千条以内的数据插入操作时,字段长度、系统保留字对SQL语句的执行速度没有影响或者影响很小;执行上万条数据插入操作时,字段长度对SQL语句的执行速度影响很小;执行十万条以上的数据操作时,系统保留字对SQL语句的执行速度影响明显。

<!--[if !supportLists]-->2、  <!--[endif]-->数据字段长度、系统保留字对系统占用的空间没有任何影响。

<!--[if !supportLists]-->3、  <!--[endif]-->在大批量数据操作时,数据类型、长度,甚至数据字段是否为系统保留字,对SQL语句的执性效率都有影响。

问题总结:

<!--[if !supportLists]-->1、  <!--[endif]-->SQL语句在执行时,将首先对相关数据表进行连接,然后进行过滤、分组、选择字段、DISTINCTORDER BY等操作。由此,我们在进行数据查询时,应尽量避免“*”连接,应考虑过滤的先后顺序。

<!--[if !supportLists]-->2、  <!--[endif]-->谨慎使用游标、触发器、索引。

<!--[if !supportLists]-->3、  <!--[endif]-->尽量避免使用系统保留字,考虑在SQL语句中区分数据字段的大小写,即SQL语句中的字段名的形式应和数据表中的字段名的形式一致。

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作