Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 1.2 Billion rows -- delete specific rows

Author  Topic 

meraydin
Starting Member

2 Posts

Posted - 2008-08-14 : 13:04:53
Hi folks

I have a table which contains 1.270 Billion rows and I want to delete almost 60% of them. I have tried many suggestions like @@rowcount=1000 while (1=1) delete ... etc. but still no luck. (I got timeout)

basically delete condition is this:

delete from MyTable where ID in (1,2,3,4)

I have an index on ID column. Any help is appreciated.

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-08-14 : 13:14:28
switch to simple recovery mode, drop the index, loop the delete, rebuild index.

_______________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.0 out!
Go to Top of Page

meraydin
Starting Member

2 Posts

Posted - 2008-08-14 : 13:21:29
Already in simple mode. Is this the only solution? Since rebuilding index on 1.2 billion records is something I want to avoid :(
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-08-14 : 13:34:55
If you are going to remove such a high percentage of the rows, it is probably faster to BCP the rows you want to keep out to a file, truncate the table, and BCP the data back in.

Another option would be to create a new table, use DTS or SSIS to copy the rows you want to keep to the new table, rename the tables when you are done, and then drop the old table.

Be sure to set a reasonable batch size, like 100,000 rows, to prevent filling the transaction log file.

Of course, the table would not be available until the operation is complete.




CODO ERGO SUM
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-08-14 : 15:17:51
I am pretty darn sure that you must have implemented table partition for 1.275 billion rows table otherwise you performance is not good. If you have it,you can switchout unwanted partition. If not, then better do in batches as Michael and spirit suggested(i.e putting in simple recovery model)
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-08-14 : 23:38:24
Partiton doesn't help if partition key is not id column. I'm second for bcp.
Go to Top of Page
   

- Advertisement -