Go Back   Rhinocerus > Newsgroup > Newsgroup comp.databases.* > Newsgroup comp.databases.ms-sqlserver

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-09-2005, 01:23 PM
kumar
Guest
 
Posts: n/a
Default Bulk Delete

we are trying to delete data from a huge 75 million records table
it takes 4hr to prune data

delete from Company where recordid in (select top 10000 recordid from
recordid_Fed3 where flag = 0)

we have a loop that prunes 10000 records at a time in a while loop
let me know if there is a better way to acheive this

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 11-09-2005, 03:09 PM
ZeldorBlat
Guest
 
Posts: n/a
Default Re: Bulk Delete

If you just want to delete *every* row in the table you can use
TRUNCATE TABLE :

<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ta-tz_2hk5.asp>

If you want to selectively delete rows, that won't work, though. If
you want to delete most of the rows (but not all of them) you could
insert the ones you want to keep into some other (temporary) table,
truncate the main table, then insert the rows back.

Reply With Quote
  #3 (permalink)  
Old 11-09-2005, 04:19 PM
Erland Sommarskog
Guest
 
Posts: n/a
Default Re: Bulk Delete

kumar (svengala@gmail.com) writes:
> we are trying to delete data from a huge 75 million records table
> it takes 4hr to prune data
>
> delete from Company where recordid in (select top 10000 recordid from
> recordid_Fed3 where flag = 0)
>
> we have a loop that prunes 10000 records at a time in a while loop
> let me know if there is a better way to acheive this


Rather than using SELECT TOP, try use a condition that matches the clustered
index and slice that up in intervals. Assume that the clustered index is
on recordid, and that this is an integer you would do:

SELECT @recordid = MIN(recordid) FROM Company (WHERE flag = 0),
@increment = 100000
WHILE EXISTS (SELECT * FROM Company WHERE recordid = @recordid)
BEGIN
DELETE Company
WHERE recordid BETWEEN @recordid AND @recordid + @increment - 1
AND flag = 0
SELECT @recordid = @recordid + @increment
END

In this way you are only scanning the table once for rows to delete.

If you anticipate that you will delete more rows than you will retain,
you could create a new table, and insert the rows to keep. In this case
you need to make sure that you also bring with you constraints, indexes,
and triggers, and you will have to move referencing foreign keys. The
insert can be further speedied up by using SELECT INTO, but SELECT INTO
may not give you a faithful copy of the table.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Data step - delete records relative to 1 or 2 previous records Schwarz, Barry A Newsgroup comp.soft-sys.sas 0 03-03-2009 08:42 PM
Re: Data step - delete records relative to 1 or 2 previous records Arthur Tabachneck Newsgroup comp.soft-sys.sas 0 03-02-2009 10:13 PM
Data step - delete records relative to 1 or 2 previous records Maria M. Corrada Newsgroup comp.soft-sys.sas 0 03-02-2009 09:43 PM
data step - delete records relative to 1 or 2 previous records Maria Corrada Newsgroup comp.soft-sys.sas 0 03-02-2009 09:27 PM
Re: Is it a good idea to delete temp dataset? ./ ADD NAME=Data _null_; Newsgroup comp.soft-sys.sas 0 01-29-2009 06:50 PM



All times are GMT. The time now is 01:16 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.