2

在我公司的一个基于 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
4

1 回答 1

2

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

CREATE PROCEDURE YourLoginProc
WITH RECOMPILE
AS
...
于 2012-09-07T20:38:21.403 回答