Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Delete records faster Expand / Collapse
Posted Friday, March 7, 2014 4:53 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 3:27 AM
Points: 368, Visits: 1,268
What is the best of removing the data from the table.
Dont want any row from the table.(this is runtime usecase,so instead of delete, going for Truncate option).
Table is having some 5 million records.
Master table and child table are inter related with forigen keys.
So remove fk and have truncate statement and again enable fk.
i have some idea of removing the data

Step1: EXEC ('ALTER TABLE ' @Child_Tbl + 'DROP CONSTRAINT ' + @FK_Name + '');
Step2: TRUINCATE TABLE @Child_Tbl;
Step3: EXEC ('ALTER TABLE ' @Child_Tb + ' ADD CONSTRAINT ' + @FK_Name + ' FOREIGN KEY (' + @Child_Col + ') REFERENCES ' @Parent_Tbl + ' (' + @Parent_Col + '));

Step1 is running and it successed to Step2 if someone kills the current session then when step2 is running
what will happen?
How to reenable the fk to table again in this case.

Is this a best option or we can go with delete operation so that even any break happen there wont be much problem(but log size will be big).

Post #1548677
Posted Friday, March 7, 2014 7:28 AM



Group: General Forum Members
Last Login: Today @ 9:21 AM
Points: 23,522, Visits: 37,755
In simple terms, wrap the three steps in an explicit transaction.

step 1;
step 2;
step 3;

There may be more to this if you are looking at adding any kind of error handling to the process. Since you appear to be using SQL Server 2000 at this point, I will leave addressing that to others as it has been years since I have worked with that version of SQL Server.

Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1548738
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse