Bol*_*n32 48 sql-server-2008 asp.net-webpages
我正在运行一个ASP.NET网页页面,在初始加载时从SQL服务器中提取项目列表.此查询在大约一秒钟内运行,并在2秒内加载页面.返回约1000条记录,给予或接受.我从Service Manager SQL数据库中提取Hostnames以及其他一些信息.
在这个页面中,我有一个内置的搜索,它基本上运行完全相同的查询,但使用基于主机名的LIKE运行它.这将加载包含搜索查询一部分的所有主机名的同一页面.查询通常在一秒钟内在SQL Management Studio中运行,但加载页面需要更长时间,有时会超时.
我的问题是,为什么基于参数的搜索需要更长的时间,有时会因为没有明显原因而超时.是否有任何步骤可以缓解此超时?以下是完整错误.
'/'应用程序中的服务器错误.
The wait operation timed out
Run Code Online (Sandbox Code Playgroud)
描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
例外细节:
System.ComponentModel.Win32Exception: The wait operation timed out
Source Error:
Line 13: }
Line 14:
Line 15: var selectedData = db.Query(selectCommand, searchTerm);
Line 16:
Line 17:
Source File: c:\Users\u0149920\Documents\My Web Sites\AppSupport\servers\default.cshtml Line: 15
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
[Win32Exception (0x80004005): The wait operation timed out]
[SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1753346
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5295154
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1682
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +59
System.Data.SqlClient.SqlDataReader.get_MetaData() +90
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +365
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) +1325
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +175
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +134
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
System.Data.Common.DbCommand.ExecuteReader() +12
WebMatrix.Data.<QueryInternal>d__0.MoveNext() +152
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
WebMatrix.Data.Database.Query(String commandText, Object[] parameters) +103
ASP._Page_servers_default_cshtml.Execute() in c:\Users\u0149920\Documents\My Web Sites\AppSupport\servers\default.cshtml:15
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +69
System.Web.WebPages.WebPage.ExecutePageHierarchy() +151
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +114
Run Code Online (Sandbox Code Playgroud)
版本信息: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
Jef*_*eff 32
您遇到的问题是查询命令花费的时间太长.我相信查询执行的默认超时是15秒.您需要设置CommandTimeout(以秒为单位),以便命令完成其执行的时间足够长."CommandTimeout"与连接字符串中的"Connection Timeout"不同,必须为每个命令设置.
在您的SQL选择事件中,使用命令:
e.Command.CommandTimeout = 60
Run Code Online (Sandbox Code Playgroud)
例如:
Protected Sub SqlDataSource1_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
e.Command.CommandTimeout = 60
End Sub
Run Code Online (Sandbox Code Playgroud)
San*_*lke 23
对于所有比我更了解的人,而不是将其标记为无益或误导,请再次阅读.由于锁定线程消耗了所有资源,因此我的虚拟机(VM)无法响应,因此杀死线程是我唯一的选择.我不建议那些运行长查询的人,但可能会帮助那些陷入无法响应VM的人.它可以接听电话.是的,它会终止您的查询,但它保存我的VM机器被销毁.
Serverstack已经回答了类似的问题.它解决了我在VM机器上使用SQL的问题.请点击这里
您需要运行以下命令来修复索引问题.
exec sp_updatestats
Run Code Online (Sandbox Code Playgroud)
如果您正在使用实体框架,则可以通过执行以下操作来扩展默认超时(以便为长时间运行的查询提供更多时间来完成):
myDbContext.Database.CommandTimeout = 300;
Run Code Online (Sandbox Code Playgroud)
myDbContext您的DbContext实例在哪里,并且300是以秒为单位的超时值.
(实体框架6中的语法current)
我遇到过同样的问题.跑步exec sp_updatestats有时会奏效,但并非总是如此.我决定NOLOCK在查询中使用该语句来加速查询.只需NOLOCK在FROM子句后添加,例如:
SELECT clicks.entryURL, clicks.entryTime, sessions.userID
FROM sessions, clicks WITH (NOLOCK)
WHERE sessions.sessionID = clicks.sessionID AND clicks.entryTime > DATEADD(day, -1, GETDATE())
Run Code Online (Sandbox Code Playgroud)
阅读完整的文章在这里.
查看数据库中的重新索引表。
您可以首先找出碎片级别 - 如果它高于 10% 左右,您可以从重新索引中受益。如果它非常高,则可能会造成严重的性能瓶颈。
这应该定期进行。