只有在重新启动SQL Server数据库服务器时,存储过程才有效

时间:2012-09-07 20:37:08

标签: c# sql-server-2005

在我公司的一个基于C#,ASP.Net,SQL Server 2005和Windows Server 2008构建的内部项目中,当有人试图登录时,登录存储过程只需要很长的时间。但是,当我重新启动数据库服务器时,问题得到解决。这发生在每天。这可能不是数据库加载问题,因为其他存储过程只是工作正常。此外,如果我在存储过程中提取查询,我会在适当的时间得到结果。在存储过程中,除了获取数据之外,存在if条件,其仅将用户限制为某些IP。

我不知道问题是什么。有人可以帮我解决问题。 提前谢谢。

这是存储过程

ALTER PROCEDURE [dbo].[get_user]          
@username varchar(50), @pwd varchar(50), @passcode varchar(50), @ipaddress varchar(15)          
AS          
BEGIN          
 IF(@ipaddress = 'aaa.aaa.aaa.aaa' OR @ipaddress = 'aaa.aaa.aaa.aaa' OR @ipaddress = 'aaa.aaa.aaa.aaa')          
 BEGIN          
  IF EXISTS(select up_id from tbl_user_profile (nolock) WHERE up_status='Y')          
  BEGIN          
   DECLARE @ri_date_time varchar(20)          
   SET @ri_date_time=convert(varchar(50),getdate(),120)          
   DECLARE @user_exists as int          
   Set @user_exists = 0          
   Set @user_exists = (Select count(*) from tbl_recent_items (nolock) where ri_user_name=@username)          
   IF @user_exists = 0          
   BEGIN          
    BEGIN TRAN          
     INSERT INTO tbl_recent_items (ri_user_name, ri_access_date_time) values (@username, @ri_date_time+'|'+@ipaddress+'^')          
     IF @@error=0 COMMIT TRAN ELSE ROLLBACK TRAN          
   END          
   ELSE          
   BEGIN          
    DECLARE @datetext varchar(max)          
    SET @datetext = (select isnull(ri_access_date_time,'') from tbl_recent_items(nolock) where  ri_user_name=@username)          
    SET @datetext=@ri_date_time+'|'+@ipaddress+'^'+@datetext          
    BEGIN TRAN          
     UPDATE tbl_recent_items Set ri_access_date_time=@datetext where ri_user_name = @username          
     IF @@error=0 COMMIT TRAN ELSE ROLLBACK TRAN          
   END          
  END          
  SELECT CONVERT(varchar(4),up_id) as up_id,LTRIM(RTRIM(isnull(up_user_name,''))) as u_user_name,           
  ISNULL(up_password,'') as u_user_password,isnull(up_first_name,'') as up_first_name,           
  ISNULL(up_middle_name,'') as up_middle_name, isnull(up_last_name,'') as up_last_name,           
  ISNULL(up_branch_id,'') as up_branch_id, isnull(b_branch_id,'') as b_branch_id,          
  ISNULL(b_branch_name,'') as b_branch_name,isnull(up_mobile_phone,'') as up_mobile_phone,          
  --ISNULL(up_pager_phone,'') as up_pager_phone,  
  isnull(up_email,'') as up_email,          
  ISNULL(up_status,'') as u_status,   
  --isnull(up_job_posting_phone,'') as up_job_posting_phone,          
--  ISNULL(up_level,'') as up_level, 
    isnull(up_trainee,'0') as up_trainee,          
  ISNULL(up_senior,'0') as up_senior, isnull(up_team_lead,'0') as up_team_lead, isnull(up_ist,'0')as up_ist, up_hyd_branch,    
  convert(varchar(10),up_password_date,102)as up_password_date    
  FROM tbl_user_profile (nolock)           
  LEFT OUTER JOIN tbl_branches (nolock) on up_branch_id = b_id where           
  up_user_name = @username and up_password =@pwd          
 END          
 IF EXISTS(select up_id from tbl_user_profile (nolock) WHERE up_user_name = @username and up_password = @pwd and up_passcode = @passcode and up_status='Y')          
 BEGIN          
  IF EXISTS(select up_id from tbl_user_profile (nolock) WHERE up_status='Y')          
  BEGIN          
   DECLARE @ri_date_time1 varchar(20)          
   SET @ri_date_time1=convert(varchar(50),getdate(),120)          
   DECLARE @user_exists1 as int          
   Set @user_exists1 = 0          
   Set @user_exists1 = (Select count(*) from tbl_recent_items (nolock) where ri_user_name=@username)          
   IF @user_exists1 = 0          
   BEGIN          
    BEGIN TRAN          
     INSERT INTO tbl_recent_items (ri_user_name, ri_access_date_time) values (@username, @ri_date_time1+'|'+@ipaddress+'^')          
     IF @@error=0 COMMIT TRAN ELSE ROLLBACK TRAN          
   END          
   ELSE          
   BEGIN          
    DECLARE @datetext1 varchar(max)          
    SET @datetext1 = (select isnull(ri_access_date_time,'') from tbl_recent_items(nolock) where  ri_user_name=@username)          
    SET @datetext1=@ri_date_time1+'|'+@ipaddress+'^'+@datetext1          
    BEGIN TRAN          
     UPDATE tbl_recent_items Set ri_access_date_time=@datetext1 where ri_user_name = @username          
     IF @@error=0 COMMIT TRAN ELSE ROLLBACK TRAN          
   END          
  END          
  SELECT CONVERT(varchar(4),up_id) as up_id,LTRIM(RTRIM(isnull(up_user_name,''))) as u_user_name,           
  ISNULL(up_password,'') as u_user_password,isnull(up_first_name,'') as up_first_name,           
  ISNULL(up_middle_name,'') as up_middle_name, isnull(up_last_name,'') as up_last_name,           
  ISNULL(up_branch_id,'') as up_branch_id, isnull(b_branch_id,'') as b_branch_id,          
  ISNULL(b_branch_name,'') as b_branch_name,isnull(up_mobile_phone,'') as up_mobile_phone,          
  --ISNULL(up_pager_phone,'') as up_pager_phone,  
  isnull(up_email,'') as up_email,          
  ISNULL(up_status,'') as u_status,  
   --isnull(up_job_posting_phone,'') as up_job_posting_phone,          
--  ISNULL(up_level,'') as up_level, 
 isnull(up_trainee,'0') as up_trainee,          
  ISNULL(up_senior,'0') as up_senior, isnull(up_team_lead,'0') as up_team_lead,isnull(up_ist,'0')as up_ist, up_hyd_branch,    
  convert(varchar(10),up_password_date,102)as up_password_date    
  FROM tbl_user_profile (nolock)           
  LEFT OUTER JOIN tbl_branches (nolock) on up_branch_id = b_id where           
  up_user_name = @username and up_password =@pwd          
 END          
END

1 个答案:

答案 0 :(得分:2)

听起来你正在为该程序缓存一个糟糕的执行计划。很难说没有看到代码,但parameter sniffing可能是一个问题。一种可能的解决方法是使用WITH RECOMPILE选项。这将强制存储过程每次重新编译并生成新的执行计划。

CREATE PROCEDURE YourLoginProc
WITH RECOMPILE
AS
...