Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to delete all of a large table in t-sql?

We've run across a slightly odd situation. Basically there are two tables in one of our databases that are fed tons and tons of logging info we don't need or care about. Partially because of this we're running out of disk space.

I'm trying to clean out the tables, but it's taking forever (there are still 57,000,000+ records after letting this run through the weekend... and that's just the first table!)

Just using delete table is taking forever and eats up drive space (I believe because of the transaction log.) Right now I'm using a while loop to delete records X at a time, while playing around with X to determine what's actually fastest. For instance X=1000 takes 3 seconds, while X=100,000 takes 26 seconds... which doing the math is slightly faster.

But the question is whether or not there is a better way?

(Once this is done, going to run a SQL Agent job go clean the table out once a day... but need it cleared out first.)

like image 349
CodeRedick Avatar asked Apr 27 '09 19:04

CodeRedick


People also ask

How do I delete a large table in SQL?

Use TRUNCATE instead of DELETE if you want to delete whole table. Try to narrow data what you want to delete and create indexes on columns to filter in data. Try to prevent logging by log backup. Move out data to a temp table what you don't want to delete, then truncate the table then insert data back.

Which is the fastest method of deleting all the data from table?

TRUNCATE TABLE is functionally identical to DELETE statement with no WHERE clause: both remove all rows in the table. But TRUNCATE TABLE is faster and uses fewer system and transaction log resources than DELETE.


2 Answers

TRUNCATE the table or disable indexes before deleting

TRUNCATE TABLE [tablename]

Truncating will remove all records from the table without logging each deletion separately.

like image 68
Joe Phillips Avatar answered Oct 20 '22 12:10

Joe Phillips


To add to the other responses, if you want to hold onto the past day's data (or past month or year or whatever), then save that off, do the TRUNCATE TABLE, then insert it back into the original table:

SELECT
     *
INTO
     tmp_My_Table
FROM
     My_Table
WHERE
     <Some_Criteria>

TRUNCATE TABLE My_Table

INSERT INTO My_Table SELECT * FROM tmp_My_Table

The next thing to do is ask yourself why you're inserting all of this information into a log if no one cares about it. If you really don't need it at all then turn off the logging at the source.

like image 27
Tom H Avatar answered Oct 20 '22 13:10

Tom H