Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple SQL Delete statement giving timeout

When executing this simple SQL Delete statement, I'm getting a timeout from SQL Server:

DELETE FROM dbo.[User] WHERE Id = 95146

There are about 95.000 records in the table.

I thought it might be because of indexes on the table, so I've deleted all except from the primary key (Id) which is clustered, but this didn't help.

I've deleted all the statistics I created as well, but also without any effect.

What else can I do to optimize for this?

Kind regards, David

like image 482
dgivoni Avatar asked Jul 28 '11 12:07

dgivoni


People also ask

How do I fix SQL timeout error?

Troubleshoot timeout expired errors If you encounter a connection-timeout error, follow the steps: Increase the connection-timeout parameter. If you use an application to connect to SQL Server, increase the relevant connection-timeout parameter values and check whether the connection eventually succeeds.

What causes a SQL query to timeout?

The timeout period elapsed prior to completion of the operation or the server is not responding. The customer has SQL Server replication and if they use the software pointing to the subscription the issue does not happen so it has to be something on the main publisher DB that is causing these timeouts.

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.


3 Answers

How many foreign keys do you have referencing the Id column of Users, and are there indexes on these columns?

If cascade is set as NO_ACTION, as you've indicated, the time may be being spent by SQL Server having to perform a full table scan on each of these tables to ensure that there's no reference to Id 95146 - I've seen this easily take minutes at a time before, if the other tables are large.

like image 189
Damien_The_Unbeliever Avatar answered Oct 17 '22 08:10

Damien_The_Unbeliever


I was struggling with a DELETE statement which was causing our ERP (Epicor 10) system to timeout on its nightly run of MRP. It was a delete with a few joins in it, and one of the tables involved in the join is quite large. The odd thing was that if I transformed the delete to a SELECT * for the same joins / where clause, then the query would complete instantly, and had ZERO results (i.e. the delete statement was obviously doing some long, large table scanning but it didn't actually find anything to delete).

What finally helped was I ran the select statment version of it with Show actual execution plan turned on, and this had a suggested Non-Unique Index to add to one of the tables. After I added this index, the select statement still ran instantly, but now the delete statement did as well!

like image 40
Adam Nofsinger Avatar answered Oct 17 '22 09:10

Adam Nofsinger


that's very odd. my guess is you have a ON DELETE CASCADE foreign key referencing your Users table elsewhere in your schema. Check your constraints:

SELECT f.name AS ForeignKey,
    OBJECT_NAME(f.parent_object_id) AS TableName,
    COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,
    OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
    COL_NAME(fc.referenced_object_id,
    fc.referenced_column_id) AS ReferenceColumnName,
    f.delete_referential_action_desc
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
like image 29
Blazes Avatar answered Oct 17 '22 08:10

Blazes