question

Arul 1 avatar image
Arul 1 asked

Slow delete statement

Simple delete query is taking more than 15 hrs. Table contains around 6 852 968 records.

Query is :

delete from wmcrs_mf_transactions where file_syscode = 56020                    

Find below for the Create table query of that table. Pls provide your valuable suggestions to close this issue. Thanks in advance.

sql-server-2000performancedelete
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

graz avatar image graz ♦ commented ·
Please edit your question to include the create table statement. Also indicate whether file_syscode has an index and how many rows you think should be deleted by this statement.
0 Likes 0 ·
Jay Bonk avatar image
Jay Bonk answered

Is there an index on the file_syscode?

How active is the table when you're trying to do the delete?

Assuming that the table has a primary key, and that you are deleteing a large number of rows, you could break the delete into a set of smaller deletes to see if that helps

DECLARE @i INT            
            
SET @i = 1            
            
WHILE @i > 0            
BEGIN            
    DELETE FROM wmcrs_mf_transactions              
    WHERE KeyValue IN (SELECT TOP 10 KeyValue FROM wmcrs_mf_transactions WHERE file_syscode = 56020)            
            
    SET @i = @@ROWCOUNT            
            
    PRINT CAST(@i AS VARCHAR(10)) + ' Rows Deleted'            
END            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Arul 1 avatar image
Arul 1 answered

Query is expected to delete around 3000 records only.

Following are the indexes created on the table..........

nonclustered located on PRIMARY >> transaction_date nonclustered located on PRIMARY >> customer_syscode nonclustered located on PRIMARY >> prematch_syscode nonclustered located on PRIMARY >> file_syscode clustered, unique, primary key located on PRIMARY >> mf_tran_syscode nonclustered located on PRIMARY >> customer_syscode, transaction_date, transaction_type, scheme_option

Also the table is having some more constraints......

I am getting following msg, when i am trying to paste the create table scripts in the forum

*  Oops! Your answer couldn't be submitted because:            
* sorry, new users can only post a maximum of one hyperlink            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kristen avatar image
Kristen answered

nonclustered located on PRIMARY >> file_syscode

Well, you've got an index on that field, so it should be quick.

1) Make sure all indexes (or at the very least this one and the clsutered index) have been recently rebuilt.

2) Other than that it will be the Constraints - which all have to be checked. Rebuild the indexes that the Constraints are relying on if that has not been done recently.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jay Bonk avatar image
Jay Bonk answered

Just another thought, you said that you were expecting to delete 3K rows from a table that has 6.8M rows and you said was taking 15 hours, as pointed out the column is indexed.

When was the last time that the statistics were updated?

Are there any delete triggers on the table that might be causing issues?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Peso avatar image
Peso answered

Also, do you have CASCADE_DELETE activated on the table?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Peso avatar image
Peso answered

Any foreign keys to from this table? I have the same problem. When I try to delete one record out of 200 million, it takes about 5 hours even if I use the PK column. When I disable/remove the FK constraints, it takes about 1 second.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.