日期:2014-05-18  浏览次数:20423 次

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


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

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




alter proc [dbo].[sp_bad]
(    
    @d varchar(5),
    @p varchar(50),
    @im varchar(20)
)
as
begin
    
    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 int
declare @needdown bit,@times2 int,@mo varchar(20)
set @flag_a=''  
set @flag_c=''
set @count=0 
set @sign=''
set @waketxt=''
set @diff=0
set @flag_url=''
set @version=''
set @times=-1    
set @needdown=0
set @times2=0



if(@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