Solved

Why does DELETE take a long time (and act so differently) when table has over 200 fields)?

Posted on 2009-05-13
7
572 Views
Last Modified: 2012-05-06
Dear Experts,

I have a perplexing problem that has confounded many at our company.  I was hoping someone
here had the answer.

I have few tables that are related to a master table.
The master table has a key seq number which is related to a number of different tables.
When I want to delete from the master and related tables the delete takes a very long time
for the tables with 200+ fields.
In Query Analyzer, I noticed that when I enter the following command

DELETE FROM DetailTable where SeqNum in (10355)
I get the following response:
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
...
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)
(0 row(s) affected)

with (0 row(s) affected) appearing up  to the number of fields in that table.
And this takes about 20 seconds to complete

If I execute the same command on a much smaller table (about 20 fields)
DELETE FROM DetailTable_2 where SeqNum in (10355)
I simply get the response
(0 row(s) affected)
within a fraction of a second.

Each table has SeqNum indexed.  So I don't know if this is a bug in SQL server, or if
there is something else that needs to be done for such a large table.

We don't have  a cascade update or delete on the table.  I know we may be able to add this,
but before I did, I thought I'd ask because adding a cascade delete may affect legacy code (which I'm trying to avoid).  I also don't want to redesign the table (into many smaller tables), again because of legacy code.

Thank you all in advance for your help!
0
Comment
Question by:BrianMc1958
7 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 450 total points
ID: 24379770
that "larger" table has for sure a trigger on it.
you might want to "disable" that trigger for your large delete ...
0
 

Author Comment

by:BrianMc1958
ID: 24379838
Yes,
You are right, I removed the trigger and it took a fraction of a second.  Of course, we want to trace
when people delete records from our database as a matter of policy.  Is there another way to handle
this situation?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24380709
>>Is there another way to handle this situation?<<
Don't allow it?

Or so it does not sound facetious, only allow DELETEs (as well as UPDATEs and INSERTs) from Stored Procedures which can only be run by users that have the appropriate EXECUTE permissions.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 50 total points
ID: 24380833
One idea is to setup replication and do the auditing on the subscriber...
0
 

Author Comment

by:BrianMc1958
ID: 24381183
Thank you all for your comments.
I do have a bit of trouble understanding why a trigger would cause such a huge problem.   I've
noticed in other large tables (that we have audit triggers on), the amount of time to execute is
doubled (not 20X as long in this instance).  I've notice that even if no records are deleted in
my query, it still takes an inordinate amount of time.

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24382440
the trigger is sql also, so all the sql statements in there take also some time.
you have to check if any of those queries are not optimized.

the suggestion of chapmandew is usually the best method, offloading the trigger to a dedicated server...
0
 

Author Closing Comment

by:BrianMc1958
ID: 31581220
Thank you all for your help.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question