由SQL Server 2005大批量数据操作想起的

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

问题提出:
      
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'

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

 

问题现象描述:

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

2、  数据字段长度、系统保留字对系统占用的空间没有任何影响。

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

问题总结:

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

2、  谨慎使用游标、触发器、索引。

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值