SQL Server 2005大批量数据的操作与实例演示


  本文标签:SQL Server 2005

  我们今天主要向大家讲述的是SQL Server 2005大批量数据的实际操作以及其使用的实例描述,以下是文章的具体介绍,望你浏览完以下的内容会有所收获  。我们首先是以问题提出的方式来对其进行讲述:

  

  在SQL Server 2005数据库中建立两个类型相同的数据表,如下

  

  1. create table test1  
  2. (  
  3. iId int identity(1,1) not null,  
  4. vTest1Code varchar(30) not null,  
  5. vName varchar(30) not null,  
  6. dDate datetime,  
  7. primary key(iId)  
  8. )   
  9. create table test2  
  10. (  
  11. Id int identity(1,1) not null,  
  12. Code varchar(30) not null,  
  13. Name varchar(30) not null,  
  14. date datetime,  
  15. primary key(Id)  
  16. )  

  

  两表所占用的系统空间

  

  1. exec sp_spaceused test1 exec sp_spaceused test2   
  2. Name Rows Reserved Data Index_size unused   
  3. Test1 0 0KB 0KB 0KB 0KB   
  4. Test2 0 0KB 0KB 0KB 0KB  

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

  执行数据插入操作

  

  --测试TEST1

  

  1. declare @startTime datetime  
  2. set @startTime=getdate()  
  3. declare @i int  
  4. set @i=1 
  5. while @i<100 
  6. begin  
  7. insert into test1(vTest1Code,vName) values(vCode+CAST(@i as varchar(10)),vName+CAST(@i as varchar(10)))  
  8. set @i=@i+1  
  9. end 

  select [语句执行花费时间(毫秒)]=datediff(ms,@startTime,getdate())

  go

  

  --测试TEST2

  

  1. declare @startTime datetime  
  2. set @startTime=getdate()  
  3. declare @i int  
  4. set @i=1 
  5. while @i<100 
  6. begin  
  7. insert into test2(Code,Name) values(vCode+CAST(@i as varchar(10)),vName+CAST(@i as varchar(10)))  
  8. set @i=@i+1  
  9. end 

  select [语句执行花费时间(毫秒)]=datediff(ms,@startTime,getdate())

  go

  

  插入耗时情况

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

  

  1. 100条 30 30   
  2. 1000条 250 250   
  3. 10000条 2623 2516   
  4. 100000条 26453 26560   
  5. 1000000条 275110 282796  

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

  

  1. exec sp_spaceused test1 exec sp_spaceused test2   
  2. Name Rows Reserved Data Index_size unused   
  3. Test1 1000098 48520KB 48272KB 192KB 56KB   
  4. Test2 1000098 48520KB 48272KB 192KB 56KB  

  问题现象描述:

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

  

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

  

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

  

  问题总结:

  

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

  

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

  

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

  

  以上的相关内容就是对SQL Server 2005大批量数据操作使用实例的介绍,望你能有所收获  。