简体   繁体   English

快速查询时SQL超时过期

[英]SQL timeout expired on fast query

I am doing some heavy lifting in an SQL database, from C#. 我正在使用C#在SQL数据库中进行一些繁重的工作。

I keep getting the following timeout exception: 我不断收到以下超时异常:

  System.Data.SqlClient.SqlException: 

Timeout expired. 超时时间已到。 The timeout period elapsed prior to completion of the operation or the server is not responding. 在操作完成之前超时或服务器没有响应。 The statement has been terminated. 该语句已终止。

I run a large number of queries (100k+), and it isn't any one specific query that causes the exception - some times I manage to finish all of them with no exception. 我运行了大量查询(超过10万个查询),而导致该异常的并不是任何一个特定的查询-有时我会设法无一例外地完成所有这些查询。

Some of the exceptions come from queries run as part of a larger transaction, some of them come from queries run on their own. 一些例外来自作为较大事务的一部分运行的查询,某些例外来自独立运行的查询。

When I run the same query directly in SQL Server Management Studio, they take less than a second to run. 当我直接在SQL Server Management Studio中运行相同的查询时,它们只需不到一秒钟的时间即可运行。

I use the samme SQLConnection object for all the queries, but new SQLCommand object for each. 我对所有查询都使用samme SQLConnection对象,但对每个查询都使用新的SQLCommand对象。

When I try using the SQL Server profiler, it just says that the batch/RPC has been completed. 当我尝试使用SQL Server Profiler时,它只是说批处理/ RPC已完成。

Why am I getting this exception at random? 为什么我会随机获得此异常?

You can try to increase timeout interval in your connection string and check once. 您可以尝试增加连接字符串中的超时间隔,然后检查一次。

Another reason could be that there are some locks in DB which are blocking your query to execute. 另一个原因可能是数据库中有一些阻止了查询的执行。 Check using the sp_lock, sp_who, sp_who2 to see if there are any locks in table. 使用sp_lock, sp_who, sp_who2检查表中是否有锁。

It could be due to blocking - perhaps the times when it does time out, it's being blocked by another process that it's waiting for to release locks. 这可能是由于阻塞-可能是超时导致的,它正被另一个等待释放锁的进程阻塞。 This would explain why it appears seemingly at random as it depends on what's going on at the time. 这可以解释为什么它似乎随机出现,因为它取决于当时的情况。

I'd agree with the other advice to check the blocking/locks, and see what is occurring during usage. 我同意其他建议来检查阻塞/锁定,并查看使用过程中发生了什么。 I would also certainly do a healthcheck on the server - check the SQL and NT logs, ensure that SQL has enough memory, and check the wait stats with a view to trying to minimise them. 我当然也会在服务器上进行运行状况检查-检查SQL和NT日志,确保SQL有足够的内存,并检查等待状态以尽量减少它们。

The most likely explanation are locks. 最可能的解释是锁。 Analyse that specially if you are doing updates, inserts and/or deletes. 如果要进行更新,插入和/或删除,请特别进行分析。 Re-arranging the queries could do the trick. 重新安排查询可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM