Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Delete records faster Expand / Collapse
Author
Message
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 @ 5:54 AM
Points: 336, Visits: 1,148
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).

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


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 1:34 PM
Points: 23,327, Visits: 32,068
In simple terms, wrap the three steps in an explicit transaction.

BEGIN TRANSACTION
step 1;
step 2;
step 3;
COMMIT;

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