2009-05-07 62 views
1

我使用asp.net,.NET 3.5,C#,和SQL Server Express 2005Asp.Net給出一個超時錯誤,同時運行一個存儲過程

我已經在SQL創建一個存儲過程,當我從SQL Server運行SP只需不到1秒即可返回結果。我也在查詢分析器中嘗試過這個查詢,並且它在不到1秒的時間內也給出了結果。但是當我嘗試從.NET(C#)調用此SP時,需要很長時間,然後發生超時錯誤。

這裏是我用來調用存儲過程的代碼:

SqlConnection con = new SqlConnection(); 

con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
con.Open(); 

SqlCommand command = new SqlCommand("spReport_SiteUsage_KP", con); 

command.CommandType = CommandType.StoredProcedure; 

command.Parameters.Add(new SqlParameter("@fromDate", SqlDbType.DateTime)); 

command.Parameters.Add(new SqlParameter("@toDate", SqlDbType.DateTime)); 

command.Parameters[0].Value = Convert.ToDateTime(DatePicker1.SelectedDate.ToShortDateString()); 

command.Parameters[1].Value = DatePicker2.SelectedDate; 

int i = command.ExecuteNonQuery(); 

con.Close(); 

存儲過程:

set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[spReport_SiteUsage_KP] 
    @fromDate datetime = null, 
    @toDate datetime = null 
AS 
    truncate table dbo.RPT_SiteUsage 

IF (@FromDate is not null and @ToDate is not null) --Hourly Report, grouped by hour 
Begin 

    insert into RPT_SiteUsage 
    select '' as ReportType, 
    'Site Usage for '+ datename(mm,@fromDate)+' '+datename(dd,@fromDate)+', '+datename(yy,@fromDate) + 
    ' To '+datename(mm,@toDate)+' '+datename(dd,@toDate)+', '+datename(yy,@toDate) as ReportTitle, 
    min(@fromDate) as FromDate,max(@toDate) as ToDate, 
    isnull(count(s.SessionId),0) VisitsTotal, 
    isnull(count(distinct(s.cookieid)),0) VisitsUnique, 
    isnull(sum(PagesVisited),0) PageViews, 
    isnull(round(avg(convert(decimal(10,2),PagesVisited)),2),0) PagePerVisit, 
    isnull(min(PagesVisited),0) MinNoPageViews, 
    isnull(max(PagesVisited),0) MaxNoPageViews, 
    isnull(round(avg(convert(decimal(10,2),TimeInSiteMinutes)),2),0) AvgTimeInSite, 
    isnull(min(TimeInSiteMinutes),0) MinTimeSpent, 
    isnull(max(TimeInSiteMinutes),0) MaxTimeSpent, 
    isnull(sum(NewPeople),0) as NewVisitors 
    from 
    dbo.UMM_UserAction ua inner join dbo.UMM_Session s on ua.SessionId=s.Sessionid 
    left join 
     (select ua.sessionId, datediff(ss,min(Actiondate),max(Actiondate))/60 TimeInSiteMinutes 
     from dbo.UMM_UserAction ua 
     where ActionDate between @fromDate and @toDate 
     group by ua.sessionid 
     ) sessionTime on ua.sessionId = sessionTime.sessionid 
    left join 
     (select ua.sessionId, 0 as NewPeople 
     from dbo.UMM_UserAction ua 
      inner join dbo.UMM_Session s on ua.SessionId=s.SessionId 
      inner join dbo.UMM_Cookie c on s.CookieId=c.CookieId 
      where ua.actiondate< @fromDate --this is the from date 
     group by UserId,ua.sessionId  
     ) Old on ua.sessionId = Old.sessionid 
    left join 
     (select ua.sessionId,count(distinct(uaP.PageEntryId)) as PagesVisited 
     from dbo.UMM_UserAction ua, 
     dbo.UMM_UserActionPageReview uaP 
     where ua.UserActionId=uaP.UserActionId 
     and ActionDate between @fromDate and @toDate 
     group by ua.sessionId 
     )pVisited on ua.sessionId = pVisited.sessionId 
    where ActionDate between @fromDate and @toDate 

    IF (select count(*) from RPT_SiteUsage)=0 
     insert into RPT_SiteUsage 
     select '(1 day)' as ReportType, 
     'Site Usage for '+datename(mm,@fromDate)+' '+datename(dd,@fromDate)+', '+datename(yy,@fromDate) + 
     ' To '+datename(mm,@toDate)+' '+datename(dd,@toDate)+', '+datename(yy,@toDate) as ReportTitle, 
     min(@fromDate) as FromDate,max(@toDate) as ToDate, 
     0 as VisitsTotal, 
     0 as VisitsUnique, 
     0 as PageViews, 
     0 as PagePerVisit, 
     0 as MinNoPageViews, 
     0 as MaxNoPageViews, 
     0 as AvgTimeInSite, 
     0 as MinTimeSpent, 
     0 as MaxTimeSpent, 
     0 as NewVisitors 

END 
+0

發佈您的存儲過程以及。 – 2009-05-07 16:13:16

+0

在什麼行上超時? con.Open(可能)或command.Execute? – 2009-05-07 16:14:35

+0

超時是comminig int i = command.ExecuteNonQuery(); – Kartik 2009-05-07 16:28:31

回答

1

嗯 - 我會說有你的連接字符串中的錯誤。請檢查一下。

+0

但是當我通過小日期範圍它工作正常,但是當我通過更多然後3個月然後它給出超時錯誤..我也嘗試在相同的大範圍(aprox一年)的SQL,它出來少於一秒。 .. – Kartik 2009-05-07 16:14:22

0

如果在查詢返回錯誤前需要很長時間,那麼連接(字符串)可能有問題。

+0

它工作正常,如果我通過小日期範圍數據 – Kartik 2009-05-07 16:15:14

+0

啊,更多信息。你能發佈異常細節嗎? – GvS 2009-05-08 07:46:55

0

這可能是緩存在proc上的壞的查詢計劃的有趣問題。特別是,如果查詢分析器中的查詢運行良好,則proc的內核就可以運行。檢查這個鏈接如何解決的情況:http://www.mssqltips.com/tip.asp?tip=1304

+0

沒有得到任何運氣..你有更多的idiea? – Kartik 2009-05-07 17:03:17

2

另一個想法,每個SqlCommand的TimeOut也是單獨控制,所以,你可以用CommandTimeOut屬性來控制它。

command.CommandTimeout = 120; 

不過,我會檢查執行計劃,看看它在哪裏浪費或佔用DB資源,我認爲這只是實驗,而不是生產。

+0

我試圖做到這一點,它是在2個薄荷糖後出現的,但是它在sql中的時間太多了,它在一秒鐘內回覆 – Kartik 2009-05-07 17:25:25

相關問題