Question

My issue at hand is that I need to remove about 60M records from a table without causing deadlocks with other processes that use this table. At this point I'm almost done removing the records using a while loop that only processes about 1M records at a time however it's taken all day.

Q1: What is the optimal way to remove large quantities of data from a table, keeping the table online and minimal impact to other resources that need to use this table in MS SQL Server 2005?

Q2: Is there a way to implement the individual row locking (rather than table locking) in SQL Server like they have in Oracle? (Note answering this may answer Q1).

A2: So as @Remus Rusanu informed me there is a way to do row level locking with a delete.

Was it helpful?

Solution

See this thread, the original poster actually did some tests and posted the most efficient method. An MVP did originally chime in with an option to actually insert the data you want to retain into a temp table and then truncate the original table and reinsert.

OTHER TIPS

I just did something like that recently. I simply created a SQL Server job that ran every 10 mins deleting a million rows. Code follows

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
DELETE TOP 1000000 FROM BIG_TABLE WHERE CreatedDate <= '20080630'

Said table in question had about 900 mil rows to start with. Didn't notice any significant performance issues.

The most efficient way is to use partition switching, see Transferring Data Efficiently by Using Partition Switching. The drawback is that it requires planning ahead in how the partitions are deployed.

If partition switching is not available then the answer depends on the actual table schema. You better post the actual schema (including all indexes and most importantly the clustered key definiton) and the criteria that qualifies the delete candidates.

As for Q2, SQL Server had row level locking since mid 90s, I don't know what you're actually asking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top