SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete taking time due to foreign keys


Delete taking time due to foreign keys

Author
Message
Krishna1
Krishna1
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2086 Visits: 556
Dear All

We have a base table and 10 other tables related with foreign keys to this. One of the foreign key table has miliion rows.
Foreign keys helps to stop delete from Base Table if there is data in any othese 10 tables with same key value that being deleted.

But because of miliion rows it takes long time to delete the row from main tables.


How to speeed up the delete query?
Roshan Jospeh
Roshan Jospeh
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6223 Visits: 2106
Check whether you have proper indexes for the child table.

Run a delete statement enabling execution plan and see whether the index is being used.

----------------------------------------------------------------------------------------------------------------------------------------------------
Roshan Joe

Jeff Moden -Forum Etiquette: How to post data/code on a forum to get the best help
Krishna1
Krishna1
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2086 Visits: 556
Created index on foreign key tables , for key column of the base table. but no luck.

Execution plan shows index seek for these tables.

After disabling the foreign keys on this table delete goes very fast
Orlando Colamatteo
Orlando Colamatteo
SSC Guru
SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)

Group: General Forum Members
Points: 105526 Visits: 14506
Krishna1 (2/20/2013)
Created index on foreign key tables , for key column of the base table. but no luck.

Not the Base Table, the Child Table needs an index on the columnthat refers to the Base Table. See, everytime a delete occurs the Engine needs to do a SELECT on the child table to ensure it is not orphaning any rows. If the column in the Child Table is not indexed that can be very detrimental to performance.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Krishna1
Krishna1
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2086 Visits: 556
I have aded index on child but still it takes long time and in execution plan it shows index seek.
Bhuvnesh
Bhuvnesh
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32722 Visits: 4079
Delete will take time as OPC mentioned above as it has to reference child tables too .

Two approaches are there.

first,
although it is not recommened if you are following strictly the FK concept BUT yes , remove the FK constraints then do the delete from parent + childs tables (CAREFULLY handle the data to avoid orphans records)

Second, take the batch approach for delete operation.

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Orlando Colamatteo
Orlando Colamatteo
SSC Guru
SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)

Group: General Forum Members
Points: 105526 Visits: 14506
Please post the actual execution plan and all involved table definitions including indexes and constraints.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)

Group: General Forum Members
Points: 159108 Visits: 11668
In the original post, it's noted that there is a base table and 10 children. I'd bet there's a missing index on the foreign key of at least one of them. Have you been able to isolate the bottleneck to a single child?


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Orlando Colamatteo
Orlando Colamatteo
SSC Guru
SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)

Group: General Forum Members
Points: 105526 Visits: 14506
Ed Wagner (2/21/2013)
In the original post, it's noted that there is a base table and 10 children. I'd bet there's a missing index on the foreign key of at least one of them. Have you been able to isolate the bottleneck to a single child?

He said it eases up when he drops one specific FK so I was thinking there might be a grandchild causing the issue, i.e. a child of the child table for which the FK was dropped.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)SSC Guru (159K reputation)

Group: General Forum Members
Points: 159108 Visits: 11668
Good point. We're back to needing the DDL.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search