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

高手,帮小弟我指导、优化一下小弟我的这2个SP!服务器总是卡.

2012-04-21 
高手,帮我指导、优化一下我的这2个SP!服务器总是卡...我们服务器每天都会有一段时间比较卡,从活动监视器看

高手,帮我指导、优化一下我的这2个SP!服务器总是卡...
我们服务器每天都会有一段时间比较卡,从活动监视器看有200个进程,大部分都是“可运行”、“已挂起”,这是不是跟我的存储过程写法有关。请高手们指导我一下,帮我优化一下看看。谢谢。


花点时间和精力帮我看下吧。谢谢

另外,我想问问看,如果启用事务,会不会减少服务器运行SP时消耗的资源??SP1:

C# code
alter proc [dbo].[sp_bad](        @d varchar(5),    @p varchar(50),    @im varchar(20))asbegin        declare @c int,@time1 datetime,@end int    declare @pros varchar(200)    set @pros=',aaa,bbb,ccc,ddd,eee,fff,'    set @c=0    select top 1 @c=cc,@time1=time1 from t_badToday where im=@im    if @c=0    begin        insert into t_badToday(d,p,im) values(@d,@p,@im)        if charindex(','+@p+',',@pros)>0            exec [sp_yun] @d,@p,@im,'','1'    --调用另一SP        else            select 0        return    end    else    begin        if @c>5        begin            select 1            return        end        else        begin            update t_badToday set cc=cc+1 where im=@im            if datediff(d,@time1,getdate())=0            begin                select 0                return             end            else            begin                if charindex(','+@p+',',@pros)>0                    exec [sp_yun] @d,@p,@im,'','1'    --调用另一SP                else                    select 0                return                end        end    end    end


SP2:
C# code
alter proc  [dbo].[sp_yun]         (              @d nvarchar(5),              @p nvarchar(30),              @im nvarchar(50),              @ip   nvarchar(20),              @type nvarchar(2)        )          as          begin           declare @flag_a nvarchar(10)  declare @flag_c nvarchar(500)  declare @count int declare @sign nvarchar(10)declare @waketxt nvarchar(1000)    declare @diff int   declare @createdate datetime        declare @flag_url nvarchar(100)  declare @version nvarchar(10)declare @times intdeclare @needdown bit,@times2 int,@mo varchar(20)set @flag_a=''  set @flag_c=''set @count=0 set @sign=''set @waketxt=''set @diff=0set @flag_url=''set @version=''set @times=-1    set @needdown=0set @times2=0if(@type='1')    -- get url begin         set @mo=''    set @flag_a=''    set @sign='old'    set @needdown=null    select top 1 @mo=mo,@needdown=needdown,@flag_a=pl,@version=version,@times=times1,@times2=times2 from t_yun where im=@im    if @mo='' or @mo is null    begin--插入记录        set @mo=''        select @mo=mo,@flag_a=lo,@createdate=chutime from t_chu where cu=@d and pr=@p and im=@im         if @mo='' or @mo is null return        if exists(select top 1 1 from t_sp where im=@im or mo=@mo) return        select top 1 @version=substring(version,2,3) from t_pro where d=@d and p=@p         if left(@version,1)='2' set @version='2.0'        insert into t_yun(d,p,im,mo,pl,version,times1,times2,intime,chtime,looptime,needdown,autotime,downtimes)        values(@d,@p,@im,@mo,@flag_a,@version,8,1,getdate(),@createdate,getdate(),1,getdate(),1)        set @needdown=1        set @sign='new'    end        if @version='2.0'    begin        set @needdown=0        if (@times is null or @times<1 or @times2 is null or @times2<1) and @sign='old'             set @needdown=0        else        begin                            if exists(select top 1 1 from t_code where isvalid=1 and  flag_a=@flag_a)            begin                set @needdown=1 --允许下载                    end        end    end    if @needdown is null    begin        select 0        return            end    else if @needdown=1    begin        if @version='2.0'        begin            select 2            return        end        else        begin            select top 1 @flag_url=url from t_auto_url  where id<14 and id>1 order by newid()            if(@flag_url is null or @flag_url='')                    begin                                      select 0                   return              end                    else             begin                 if @sign='old' and left(@p,4)!='d001'                begin                    update t_yun set needdown=0,autotime=getdate(),downtimes=downtimes+1 where im=@im                end                select @flag_url            end         end    end    else    begin        select 0        return    endendend      



[解决办法]
另外,我想问问看,如果启用事务,会不会减少服务器运行SP时消耗的资源??

事务是要么执行 要么回滚 一样的消耗资源
[解决办法]
没看出哪里可以再可优化的
[解决办法]
优化表结构,建立必要的索引,尽量不要再数据库中拼SQl
[解决办法]
得根据数据结构、业务逻辑设计,没有魔术师

热点排行