views:

82

answers:

2

I'm using SQL2008 and .NET 4.

The following UPDATE query causes timeout errors.

Table Pages has 600,000 records.

Error:

System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

Query:

UPDATE  Pages
SET Checked = 1  , LastChecked = GETDATE()
OUTPUT Inserted.ID, Inserted.Title INTO @Updated
WHERE ID in 
 (SELECT TOP (@Count) ID 
  FROM Pages 
  WHERE Checked = 0 and FileID = @FileID 
  ORDER BY ID)

SELECT * FROM @Updated  
END

On a different thread I Insert records to Pages table, this runs continually (a page is added every 1 sec or less).

INSERT INTO Pages (Title ,.......)
VALUES (@Title , .......)

Why do I get the timeout error and how can I solve it?

Doesn't SQL wait indefinitely on blocking situation ?

+1  A: 

It's your command/client that's timing out, rather than SQL Server itself.

Perhaps adjust your SqlCommand.CommandTimeout to a higher value?

 myCmd.CommandTimeout = 360; //seconds

Updates:

Are you sure your 1 second UPDATE statement is the one being blocked/timing out? Perhaps there's something else executing just previous?

Have you looked at SSMS to determine what is being blocked/blocking? Right click your server, select 'Activity Monitor'. alt text

What does SQL Profiler show you as being executed?

p.campbell
thx. i think adjusting the timeout isn't really a solution.
sharru
Its just too easy to extend the timeout every time the query performances to slow . i think i should fully investigate if there is something to improve.
sharru
@p.campbell: "Have you looked at SSMS to determine what is being blocked/blocking?" , can you please explain how to do this ?
sharru
@p.campbell: thx for the update. the exception is thrown randomly every 10-100 mintues. is there some way to log the blocking events ?
sharru
@sharru: what does SQL Profiler show as being sent to the server? Are you using LINQ To SQL, or ADO.NET commands? Is the exception thrown randomly for just this query, or other queries to the DB server?
p.campbell
A: 

As (+1) p.campbell said, the timeout is coming from .NET, not from SQL.

What indexing do you have in place? Without suitable coverage of columns "Checked" and/or "ID", your subquery (SELECT TOP...) will have to read every page in the table, to first get all where Checked = 0 and then sort those by ID. If it's large and being regularly updated that could make it Delay Central.

If they don't have to be ordered by Id, taking out that ORDER BY might cause the query to only read rows until it gets @Count of them.

If you have SQL 2008, a filtered query (...WHERE Checked = 0) might speed things up.

Philip Kelley
Executing the query from SQL Server management studio takes 1 second and uses non-clusteed index (cost 100%).
sharru
Sounds like you have good indexes in place.
Philip Kelley
Yes that correct , but i still get timeout....:(
sharru
If it works properly through SSMS but not through your .net code, then I'd focus on the .net code. It may be a locking/blocking problem (as per p.campbell), but odds are it'll be somthing to do with your connectivity layer, something I don't have that much experience with.
Philip Kelley