简体   繁体   中英

SQL timeout expired on fast query

I am doing some heavy lifting in an SQL database, from C#.

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.

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.

I use the samme SQLConnection object for all the queries, but new SQLCommand object for each.

When I try using the SQL Server profiler, it just says that the batch/RPC has been completed.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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