Solved

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

Posted on 2009-05-13
7
569 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 142

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 142

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now