Delete 5 lac records from table

Delete 5 lac records from table

I have a table where there is a column called nationality having possible values "US" and "Non -US'.I need to delete the "Non -US' records from my table which amounts to more than 5 lac records, in the best possible way considering the database performance.Kindly suggest the ways to do that.

Hi there, in terms of performance, one thing you'll need - in order to prevent issues with other processes, even when they will not be touching the records in question is lock escalation. Very briefly: Initially, your SQL Server engine will look at either taking out row or page locks on the table concerned. However, locks have a cost in terms of management and resource allocation - they're, to a degree, expensive. Therefore, after a certain threshold, where the Engine considers locking the individual records / pages in question too expensive, it will escalate the lock to a table lock. This prevents other processes accessing those records, due to an inability to take out their own locks. The approximate threshold per operation is c. 5000 locks (3000 pre 2005).

If there aren't too many foreign keys and the amount you're deleting is more than the amount of rows you want to keep, it may well be faster to insert the rows you want to keep into a new table, drop the old table and then rename the new one (and put all the keys and indexes back afterwards)

If there aren't too many foreign keys and the amount you're deleting is more than the amount of rows you want to keep, it may well be faster to insert the rows you want to keep into a new table, drop the old table and then rename the new one (and put all the keys and indexes back afterwards)

That's a good idea. But should I include it in a transaction? If you can post a sample code, it will be ideal. Thanks

If there aren't too many foreign keys and the amount you're deleting is more than the amount of rows you want to keep, it may well be faster to insert the rows you want to keep into a new table, drop the old table and then rename the new one (and put all the keys and indexes back afterwards)

That's a good idea. But should I include it in a transaction? If you can post a sample code, it will be ideal. Thanks

Can you describe the table a bit more? For example, what are the total number of rows that it has? How many foreign keys does it have pointing at other tables and how many foreign keys point at the table being deleted from? How much total disk space does the table occupy?

All these questions will help tell me if you can do the ol' trick of creating a temporary file group to isolate the good rows on, truncate the original table, and copy the rows back in a minimally logged fashion (for performance and to keep from blowing the log file out), and then dropping the temporary file group to keep from unnecessarily increasing the size of the MDF file. It's nearly the same as what Gail mentioned but has the advantage of not expanding the MDF file any further.

--Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair