0

I am going to delete millions of rows in each batch which I cannot delete them together since there are too many rows. I was thinking of deleting rows with no log but I did bot find anything. Is there anybody could help me to write a delete no log query?

My query would be like:

DELETE
FROM MyTable
WHERE TO_CHAR(MyDate, 'YYYYMM') = 202101

even I jus consider one month(It is about 6 million records) I cannot delete them

4
  • 1
    Does your DB have Partitioning licence? Commented Oct 25, 2021 at 10:13
  • 2
    Please abandon the idea of circumventing the redo logs. If something goes wrong you have a good chance of ending up with an unusable database. If you can't remove the rows monthly-wise, why not day-wise? A non-working method is always worse than a working method. How many rows would remain after the delete? Else renaming the table and copying the rows you'd like to keep to a new table with the old name could be an option Commented Oct 25, 2021 at 10:16
  • How many rows are in the table? How many of these rows are you going to delete? One month = 6M rows ? Or several months (a multiple of 6M)? Is your issue that you are getting "ORA-01555: snapshot too old"? Does the table have child tables (i.e. tables with foreign key constraints in this table)? Are there delete triggers on the table? Commented Oct 25, 2021 at 10:39
  • I am going to delete them little by little and then insert same data with some changes(I know I can update them but they have to many problems)in total my db has around 5 billions records and yes it has partition. in just month 01 we have around 6M. deleting data from this table took a lot of time and I think my table will be locked after some hours. Commented Oct 25, 2021 at 11:09

1 Answer 1

3

Delete operations are always logging, and also require a lot of undo, that is why they could be expensive in terms of performance when a huge number of rows are involved. Also, if you have indexes, the rows must be deleted there too.

There are several options, but they strongly depend of your environment. I would try two approaches

I am using parallel with degree 8. If you have less or more CPU in your database server, you could play with the degree. I would not use too much degree in the delete, but I would use as much as possible in the option 2

Option 1 ( use parallel dml )

alter session enable parallel dml ;
delete /*+parallel(a,8) */ from myTable a where TO_CHAR(MyDate, 'YYYYMM') = 202101 ;
commit;

Option 2 ( use CTAS and Truncate/Insert )

alter session enable parallel dml;
alter session enable parallel ddl;

create table MyBkpTable parallel compress nologging pctfree 0 
as select /*+parallel(a,8) */ * from MyTable a 
where TO_CHAR(MyDate, 'YYYYMM') != 202101 ;

truncate table MyTable reuse storage ;

insert /*+append parallel(a,8) */ into MyTable a 
select /*+parallel(b,8) */ * from MyBkpTable b;

commit;

If you have a lot of indexes on the table, mark all indexes as unusable and use alter session set skip_unusable_indexes=true before running the insert append. Then at the end, rebuild all indexes.

Update

As the table is partitioned, to benefit from partition pruning you might use ( as long as the partition key is MyDate )

delete /*+parallel(a,8) */ from myTable a where MyDate >= date '2021-01-01' and MyDate < date '2021-02-01' ;

or even better, just truncate all those partitions

alter table myTable truncate partition <<partition_name>>
Sign up to request clarification or add additional context in comments.

3 Comments

where MyDate >= date '2021-01-01' and MyDate < date '2021-02-01' to leverage partitioning. For the second approach partitioning may also be used: exchange partition with rebuild of indexes instead of insert ... select...
@astentx, I saw the last comment now, when he say that the table has partitions. when I wrote the answer, I did not know whether the table was or not partitioned
Filter on pure date is more reliable than to_char in any case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.