首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

关于数据库执行速度的有关问题

2012-01-28 
关于数据库执行速度的问题下面这段代码是把一个数据库(JJGovOA)的四张表的记录插入另一个数据库(JJGovOABa

关于数据库执行速度的问题
下面这段代码是把一个数据库(JJGovOA)的四张表的记录插入另一个数据库(JJGovOABackup)的相同的四张表中.这四张表中分别有一万条符合条件的记录(也就是说进行了一万次循环),当我一次循环只执行一次记录1时,只需要9秒钟时间.当我一次循环同时执行一次记录1和记录2时,却耗时36秒,如果一次循环四条同时执行一次,却耗掉了十几分种时间,这是为什么呢?按理说,当一次循环只执行一次记录1时,一万次循环就执行了一万次记录1,同时执行记录1和记录2时,一万次循环就相当于执行了二万次插入操作,那也应该只需要18秒呀.为什么就要36秒了? 
Declare @DocumentID varchar(30)  
declare cursor_user cursor for select DocumentID from JJGovOA.dbo.Office_Send where  
Datediff(day,WriteDate,Dateadd(month,-6,getdate())) >=1  
open cursor_user --打开游标 
fetch cursor_user into @DocumentID  
while @@Fetch_Status=0 --该循环进行了一万次. 
Begin  

  --记录1 
insert into JJGovOABackup.dbo.Office_Send select * from JJGovOA.dbo.Office_Send where  
DocumentID=@DocumentID  
  --记录2 
insert into JJGovOABackup.dbo.Office_SendExt select * from JJGovOA.dbo.Office_SendExt where  
 DocumentID=@DocumentID 
  --记录3 
insert into JJGovOABackup.dbo.Office_FlowStep select * from JJGovOA.dbo.Office_FlowStep where  
DocumentID=@DocumentID  
  --记录4 
insert into JJGovOABackup.dbo.Office_FlowHead select * from JJGovOA.dbo.Office_FlowHead where  
 DocumentID=@DocumentID  

fetch cursor_user into @DocumentID  
end  
close cursor_user  
deallocate cursor_user

[解决办法]
大哥遊標不要用這個嚴重影響性能! 你要什麽需求??
[解决办法]
这么多的数据,用游标很慢的

SQL code
--记录1  select * into JJGovOABackup.dbo.Office_Send from JJGovOA.dbo.Office_Send where  DocumentID=@DocumentID   --记录2  select * into JJGovOABackup.dbo.Office_SendExt from JJGovOA.dbo.Office_SendExt where  DocumentID=@DocumentID --记录3  select * into JJGovOABackup.dbo.Office_FlowStep from JJGovOA.dbo.Office_FlowStep where  DocumentID=@DocumentID  --记录4  select * into JJGovOABackup.dbo.Office_FlowHead from JJGovOA.dbo.Office_FlowHead where  DocumentID=@DocumentID 

热点排行