Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently remove all rows from a table in DB2

I have a table that has something like half a million rows and I'd like to remove all rows.

If I do simple delete from tbl, the transaction log fills up. I don't care about transactions this case, I do not want to rollback in any case. I could delete rows in many transactions, but are there any better ways to this?

How to efficiently remove all rows from a table in DB2? Can I disable the transactions for this command somehow or is there special commands to do this (like truncate in MySQL)?

After I have deleted the rows, I will repopulate the database with similar amount of new data.

like image 779
Juha Syrjälä Avatar asked Sep 01 '10 09:09

Juha Syrjälä


People also ask

What is the fastest way to delete all of the rows in a table?

TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column. If you want to retain the identity counter, use DELETE instead.


2 Answers

It seems that following command works in newer versions of DB2.

TRUNCATE TABLE someschema.sometable IMMEDIATE  
like image 60
Juha Syrjälä Avatar answered Oct 20 '22 19:10

Juha Syrjälä


To truncate a table in DB2, simply write:

alter table schema.table_name activate not logged initially with empty table

From what I was able to read, this will delete the table content without doing any kind of logging which will go much easier on your server's I/O.

like image 24
Luka Milani Avatar answered Oct 20 '22 18:10

Luka Milani