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
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.
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.
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.
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.
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With