Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Sql Query Timeout

Tags:

sql

sql-server

I have RO access on a SQL View. This query below times out. How to avoid this?

select     count(distinct Status)   from    [MyTable]  with (NOLOCK) where    MemberType=6 

The error message I get is:

Msg 121, Level 20, State 0, Line 0

A transport-level error has occurred when receiving results from the server (provider: TCP Provider, error: 0 - The semaphore timeout period has expired.)

like image 638
R.D Avatar asked May 12 '09 16:05

R.D


People also ask

How do I stop SQL query timing out?

Using SQL Server Management Studio Connect to MS SQL server via SQL Management Studio. In Object Explorer, right-click on the server name and then select Properties. In the new tab, click on Connections node. In Remote Query Timeout change it to your desired value or specify 0 to set no limit.

How do I change the command timeout in SQL?

Select Query Execution from tree on left side and enter command timeout in "Execute Timeout" control. Changing Command Timeout in Server: In the object browser tree right click on the server which give you timeout and select "Properties" from context menu. you can set the value in up/down control.

What is SQL timeout exception?

public class SQLTimeoutException extends SQLTransientException. The subclass of SQLException thrown when the timeout specified by Statement has expired. This exception does not correspond to a standard SQLState.


2 Answers

Your query is probably fine. "The semaphore timeout period has expired" is a Network error, not a SQL Server timeout.

There is apparently some sort of network problem between you and the SQL Server.

edit: However, apparently the query runs for 15-20 min before giving the network error. That is a very long time, so perhaps the network error could be related to the long execution time. Optimization of the underlying View might help.

If [MyTable] in your example is a View, can you post the View Definition so that we can have a go at optimizing it?

like image 195
codeulike Avatar answered Sep 25 '22 10:09

codeulike


Although there is clearly some kind of network instability or something interfering with your connection (15 minutes is possible that you could be crossing a NAT boundary or something in your network is dropping the session), I would think you want such a simple?) query to return well within any anticipated timeoue (like 1s).

I would talk to your DBA and get an index created on the underlying tables on MemberType, Status. If there isn't a single underlying table or these are more complex and created by the view or UDF, and you are running SQL Server 2005 or above, have him consider indexing the view (basically materializing the view in an indexed fashion).

like image 30
Cade Roux Avatar answered Sep 23 '22 10:09

Cade Roux