我这个存储过程 因为数据量太大 百万条数据 执行速度太慢了
有哪个高手给优化一下 谢谢!
/*CREATE PROCEDURE peoplechange
AS
set nocount on
begin
declare @sqlStr varchar(5000)
declare @oldBaseName varchar(50)
declare @newBaseName varchar(50)
set @oldBaseName='icme'
set @newBaseName='icme2'
while @@fetch_status = 0
begin
-------插入活动表--先执行删除操作 select @sqlStr ='delete from '+@oldBaseName+'..活动表'select @sqlStr =@sqlStr +' where 人员编号 in (select 人员编号 from '+@newBaseName+'..活动表) and 内容 in(select 内容 from '+@newBaseName+'..活动表)
and 类别 in(select 类别 from '+@newBaseName+'..活动表)and 学科 in (select 学科 from '+@newBaseName+'..活动表)and 形式 in(select 形式 from '+@newBaseName+'..活动表)
and 学分 in (select 学分 from '+@newBaseName+'..活动表 ) and 学时 in (select 学时 from '+@newBaseName+'..活动表 )and 日期 in(select 日期 from '+@newBaseName+'..活动表) '
---再执行插入操作
select @sqlStr =@sqlStr +'insert into '+@newBaseName+'..活动表'
select @sqlStr =@sqlStr +'( id,人员编号,内容,类别,学科,形式,学分,学时,日期,备注,录入方式,授分单位,审核状态,是否有效,状态) '
select @sqlStr =@sqlStr +'select left(newid(),35),isnull(icme..活动表.人员编号,'' ''),isnull(icme..活动表.内容,'' ''),isnull(icme..活动表.类别,'' ''),
isnull(icme..活动表.学科,'' ''),isnull(icme..活动表. 形式,'' ''),isnull(convert(char,icme..活动表.学分),'' ''),isnull(convert(char,icme..活动表.学时),'' ''),
isnull(icme..活动表.日期,'' ''),''统一调动'',0,授分单位,0,0,0 '
select @sqlStr =@sqlStr +'from '+@oldBaseName+'..活动表' exec(@sqlStr)
end
end*/
有哪个高手给优化一下 谢谢!
/*CREATE PROCEDURE peoplechange
AS
set nocount on
begin
declare @sqlStr varchar(5000)
declare @oldBaseName varchar(50)
declare @newBaseName varchar(50)
set @oldBaseName='icme'
set @newBaseName='icme2'
while @@fetch_status = 0
begin
-------插入活动表--先执行删除操作 select @sqlStr ='delete from '+@oldBaseName+'..活动表'select @sqlStr =@sqlStr +' where 人员编号 in (select 人员编号 from '+@newBaseName+'..活动表) and 内容 in(select 内容 from '+@newBaseName+'..活动表)
and 类别 in(select 类别 from '+@newBaseName+'..活动表)and 学科 in (select 学科 from '+@newBaseName+'..活动表)and 形式 in(select 形式 from '+@newBaseName+'..活动表)
and 学分 in (select 学分 from '+@newBaseName+'..活动表 ) and 学时 in (select 学时 from '+@newBaseName+'..活动表 )and 日期 in(select 日期 from '+@newBaseName+'..活动表) '
---再执行插入操作
select @sqlStr =@sqlStr +'insert into '+@newBaseName+'..活动表'
select @sqlStr =@sqlStr +'( id,人员编号,内容,类别,学科,形式,学分,学时,日期,备注,录入方式,授分单位,审核状态,是否有效,状态) '
select @sqlStr =@sqlStr +'select left(newid(),35),isnull(icme..活动表.人员编号,'' ''),isnull(icme..活动表.内容,'' ''),isnull(icme..活动表.类别,'' ''),
isnull(icme..活动表.学科,'' ''),isnull(icme..活动表. 形式,'' ''),isnull(convert(char,icme..活动表.学分),'' ''),isnull(convert(char,icme..活动表.学时),'' ''),
isnull(icme..活动表.日期,'' ''),''统一调动'',0,授分单位,0,0,0 '
select @sqlStr =@sqlStr +'from '+@oldBaseName+'..活动表' exec(@sqlStr)
end
end*/
and 类别 in(select 类别 from '+@newBaseName+'..活动表)and 学科 in (select 学科 from '+@newBaseName+'..活动表)and 形式 in(select 形式 from '+@newBaseName+'..活动表)
and 学分 in (select 学分 from '+@newBaseName+'..活动表 ) and 学时 in (select 学时 from '+@newBaseName+'..活动表 )and 日期 in(select 日期 from '+@newBaseName+'..活动表) '
改成这个看看select @sqlStr ='delete' +@oldBaseName+' from '+@oldBaseName+'..活动表 ,'+@newbasename +'..活动表 ' +'where '+@oldbasename+'.人员编号='+@newbasename+'.人员编号 and'+ +@oldbasename+'.人员编号='+@newbasename+'.人员编号 and'+
+@oldbasename+'.内容='+@newbasename+'.内容 and'+
+@oldbasename+'.类别='+@newbasename+'.类别 and'+
+@oldbasename+'.学科='+@newbasename+'.学科 and'+
+@oldbasename+'.学分='+@newbasename+'.学分 and'+
+@oldbasename+'.学时='+@newbasename+'.学时 '
1,删除新表中和旧表人员编号,内容,类别,学科,形式,学分,学时,日期,相同的数据
2,将旧表的数据用新的ID,插入到新表中
关于第一部分的代码可以优化,你最终应该是用这个sql来删数据的,修改一下你的拼接语句吧
delete a
from icme..活动表 a join icme2..活动表 b
on a.人员编号=b.人员编号 and a.内容=b.内容 and a.类别=b.类别 and a.学科=b.学科 and a.形式=b.形式 and a.学分=b.学分 and a.学时=b.学时 and a.日期=b.日期第二部分应该影响不大