How to Delete more than 10 million normal heap table data from Oracle Database Delete

Source: Internet
Author: User

How to Delete more than 10 million normal heap table data from Oracle Database Delete

Requirement: delete an Oracle database to delete 10 million historical records from a common heap table.

Impact of direct deletion:

1. The deletion may fail due to insufficient undo tablespace;

2. The undo tablespace may be excessively used, affecting the normal operation of other users.

Improvement Solution: submit each delete 1 K rows. (This splits a big thing into several small things)

Note: The following method takes deleting all records before January 1, 2014 as an example. Modify the settings based on your actual situation to prevent misoperation.

Method 1

Declare
Cursor [del_cursor] is select a. *, a. rowid row_id from [table_name] a order by a. rowid;
Begin
For v_cusor in [del_cursor] loop
If v_cusor. [time_stamp] <to_date ('2017-01-01 ', 'yyyy-mm-dd') then
Delete from [table_name] where rowid = v_cusor.row_id;
End if;
If mod ([del_cursor] % rowcount, 1000) = 0 then
Commit;
End if;
End loop;
Commit;
End;

Variable description in Method 1:

[Del_cursor] cursor name

[Table_name] Name of the table to be deleted

[Time_stamp] Name of the time field of the table you used as the filter Condition

Method 2

Declare
Maxrows number default 1000;
Delete_ct number default 0;
Begin
Select count (1)/maxrows into delete_ct from [table_name] where [time_stamp] <to_date ('2017-01-01 ', 'yyyy-mm-dd ');
For I in 1 .. TRUNC (delete_ct) + 1
Loop
Delete [table_name] where [time_stamp] <to_date ('1970-01-01 ', 'yyyy-mm-dd') and rownum <= maxrows;
Commit;
End loop;
End;

Variable description in Method 2:

[Table_name] Name of the table to be deleted

[Time_stamp] Name of the time field of the table you used as the filter Condition

Note

The core idea of the two methods is to split a big thing into several small things, no matter which method is used, we recommend that you test in the corresponding test environment before considering whether it can be used in actual production.

By the way, whether such a large table can be transformed into a partition table should be considered comprehensively.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.