当前位置: 代码迷 >> Sql Server >> ,使用游标速度太慢
  详细解决方案

,使用游标速度太慢

热度:62   发布时间:2016-04-27 14:58:57.0
求助,使用游标速度太慢
SQL code
ALTER proc [dbo].[p_multipleChoiceSplit]asdeclare @Code varchar(50),@s_chronic varchar(100),@s_CHSISRequire varchar(100),@s_HealthCheckProblem varchar(100)---记录数组的长度declare @length int,@next int---对表2B-7,3A-4,3B-3b多选项进行拆分declare cur_list cursor for select Code,IsHaveChronic,CHSISRequire,HealthCheckProblem from dbo.PeopleQuestion where IsHaveChronic!='' or CHSISRequire!='' or HealthCheckProblem!=''begin    ---建立拆分的多选项表    if not exists(select name from sys.tables where name='t_multipleChoiceSplit' and type='u')        create table t_multipleChoiceSplit(Code varchar(50) null,[Type] varchar(50) null ,[Option] varchar(50) null)    open cur_list    fetch next from cur_list into @Code,@s_chronic,@s_CHSISRequire,@s_HealthCheckProblem    while @@fetch_status=0    begin        ---2B-7                if (@s_chronic!='')        begin            set @length=dbo.Get_StrArrayLength(@s_chronic,'.')            set @next=1            if not exists(select Code from t_multipleChoiceSplit where [email protected] and [Type]='IsHaveChronic')            begin                while @next<[email protected]                begin                    insert into t_multipleChoiceSplit values(@Code,'IsHaveChronic',dbo.Get_StrArrayStrOfIndex(@s_chronic,'.',@next))                    set @[email protected]+1                end            end        end        ----3A-4         if (@s_CHSISRequire!='')        begin            set @length=dbo.Get_StrArrayLength(@s_CHSISRequire,'.')            set @next=1            if not exists(select Code from t_multipleChoiceSplit where [email protected] and [Type]='CHSISRequire')            begin                while @next<[email protected]                begin                    insert into t_multipleChoiceSplit values(@Code,'CHSISRequire',dbo.Get_StrArrayStrOfIndex(@s_CHSISRequire,'.',@next))                    set @[email protected]+1                end            end        end        ----3B-3b        if(@s_HealthCheckProblem!='')        begin            set @length=dbo.Get_StrArrayLength(@s_HealthCheckProblem,'.')            set @next=1            if not exists(select Code from t_multipleChoiceSplit where [email protected] and [Type]='HealthCheckProblem')            begin                while @next<[email protected]                begin                    insert into t_multipleChoiceSplit values(@Code,'HealthCheckProblem',dbo.Get_StrArrayStrOfIndex(@s_HealthCheckProblem,'.',@next))                    set @[email protected]+1                end            end        end        fetch next from cur_list into @Code,@s_chronic,@s_CHSISRequire,@s_HealthCheckProblem    end    select distinct * into #t_multipleChoiceSplit from t_multipleChoiceSplit    delete from t_multipleChoiceSplit    insert into t_multipleChoiceSplit select * from #t_multipleChoiceSplit    close cur_list    deallocate cur_listend



dbo.PeopleQuestion 表里有9W行数据,整个执行完要3个小时,有没有好办法优化一下或者不用游标会不会快点,实在太慢了,大家帮忙看看。

------解决方案--------------------
最好把函数 Get_StrArrayLength 也贴出来.
  相关解决方案