从生产站点上的ASP.NET前端环境调用此存储过程时,我有中间超时。它返回以下sql异常:
异常详细信息:System.Data.SqlClient.SqlException:Timeout 过期。在完成之前经过了超时时间 操作或服务器没有响应。
有趣的是,在服务器上执行此过程时,或使用Management Studio从远程PC执行此过程时,它将在6秒内执行。但是,有时候从ASP.NET应用程序执行时会超时吗?这个查询可以改进吗?或者这个问题与其他问题有关吗?有谁可以提供帮助?我已经阅读了一些关于增加超时并在我的web.config中启用连接字符串池的线程,但还没有尝试过。
ALTER PROCEDURE [dbo].[Report_Activity]
(
@StartDate DATETIME
, @EndDate DATETIME
, @TotalActions INT OUTPUT
)
AS
BEGIN
SELECT @TotalActions = COUNT(EventHistoryId)
FROM dbo.SessionEventHistory
WHERE DateCreated BETWEEN @StartDate AND @EndDate
SELECT EventDescription, COUNT(EventHistoryId) AS EventCount
FROM dbo.SessionEventHistory
WHERE DateCreated BETWEEN @StartDate AND @EndDate
GROUP BY EventDescription
ORDER BY EventDescription
SQL SCHEMAS:
CREATE TABLE [dbo].[SessionEventHistory](
[EventHistoryID] [int] IDENTITY(1,1) NOT NULL,
[SessionHistoryID] [int] NOT NULL,
[CategoryID] [int] NULL,
[UserName] [nvarchar](50) NULL,
[IPAddress] [nvarchar](20) NOT NULL,
[EventDescription] [nvarchar](1000) NOT NULL,
[EventData] [varbinary](max) NULL,
[DateCreated] [datetime] NOT NULL,
CONSTRAINT [PK_UserEventHistory] PRIMARY KEY CLUSTERED
(
[EventHistoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[SessionEventHistory] ADD CONSTRAINT [DF_UserEventHistory_DateCreated] DEFAULT (getdate()) FOR [DateCreated]
GO
表有3个索引:
/****** Object: Index [IX_SessionEventHistory_SessionHistoryId_CategoryId] Script Date: 07/04/2012 10:47:06 ******/
CREATE NONCLUSTERED INDEX [IX_SessionEventHistory_SessionHistoryId_CategoryId] ON [dbo].[SessionEventHistory]
(
[SessionHistoryID] ASC,
[CategoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object: Index [IX_SessionEventHistory_UserName_DateCreated] Script Date: 07/04/2012 10:47:09 ******/
CREATE NONCLUSTERED INDEX [IX_SessionEventHistory_UserName_DateCreated] ON [dbo].[SessionEventHistory]
(
[UserName] ASC,
[DateCreated] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object: Index [PK_UserEventHistory] Script Date: 07/04/2012 10:47:14 ******/
ALTER TABLE [dbo].[SessionEventHistory] ADD CONSTRAINT [PK_UserEventHistory] PRIMARY KEY CLUSTERED
(
[EventHistoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
修改
我添加了以下索引,它看起来没问题吗?
CREATE NONCLUSTERED INDEX [IX_SessionEventHistory_DateCreated] ON [dbo].[SessionEventHistory]
(
[DateCreated] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
答案 0 :(得分:4)
查看sys.dm_exec_sessions
以获取ASP.Net应用程序和SSMS会话。我猜测至少有一个SET
设置不同。这可能导致不同的计划(最终这归因于参数嗅探),并且应用程序方面通常会因此而变得更糟。
有关详细信息,请参阅其他问题:
Stored procedure slow when called from web, fast from Management Studio
Procedure times out from ADO.NET but not in SSMS
Query times out when executed from web, but super-fast when executed from SSMS
ADO .NET vs. SQL Server Management Studio - ADO performs worse
答案 1 :(得分:0)
也许这是一个糟糕的执行计划,通过一些非典型的SP调用创建。
在执行SP或定义时尝试使用WITH RECOMPILE
。
EXEC [dbo].[Report_Activity] @StartDate, @EndDate, @TotalActions WITH RECOMPILE
或
ALTER PROCEDURE [dbo].[Report_Activity]
(
@StartDate DATETIME
, @EndDate DATETIME
, @TotalActions INT OUTPUT
)
WITH RECOMPILE
AS
...
您还可以运行sp_recompile
来标记您的SP,以便在下次执行时进行重新编译。