|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 12:51 AM
Points: 127,
Visits: 277
|
|
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?
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 3:45 AM
Points: 572,
Visits: 1,157
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 12:51 AM
Points: 127,
Visits: 277
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:26 PM
Points: 6,696,
Visits: 11,715
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 12:51 AM
Points: 127,
Visits: 277
|
|
I have aded index on child but still it takes long time and in execution plan it shows index seek.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562,
Visits: 3,451
|
|
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---------- While 1 = 1 (Learning SQL....) Click to get fast response of your post
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:26 PM
Points: 6,696,
Visits: 11,715
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 6:44 AM
Points: 1,856,
Visits: 528
|
|
| 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?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:26 PM
Points: 6,696,
Visits: 11,715
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 6:44 AM
Points: 1,856,
Visits: 528
|
|
| Good point. We're back to needing the DDL.
|
|
|
|