Online purging historical data efficiently using a single command in Oracle Database

Categories: Oracle Database · Storage

Eventually, we are requested to delete some historical data from tables and if you are not aware of a few tricks you are probably in a huge mess. Deleting rows from a table generates a lot of undo and redo data. Undo data to support the rollback operation and redo data to guarantee the durability of your data and the undo data just generated for any recovery scenarios. When a lot of redo data is being generated means your database, that is expected to be in archive log mode, will generate a lot of archive log files. You might not be prepared for the amount of space required and so on… The space required for the archive log files will, obviously, depend on the amount of data you are deleting.

My point here is that, depending on your level of expertise, you probably know that, for example in a scenario you are keeping only 30% of the original data, you can CTAS just the portion of data you are keeping, drop the former table and rename it back to its original name. This way is much better and faster than a delete statement. I will not demonstrate that here as you can easily find articles on the web about that. If you do not know what I am talking about please read this one: Deleting many rows from a big table. Another option is dropping partitions if your table is partitioned, but partitioning requires Enterprise Edition and extra licensing. I am presenting here an operation that can be executed in any Oracle Database Enterprise Edition version 12.2 or above without the need of any extra license. Maybe by removing the “online” clause from the statement you can also run this in Standard Edition, but please check the licensing guide first and test.

First of all, let’s connect to our database. Here I am using my Autonomous Transaction Processing Database TESTDB:

[Frankys-MacBook-Pro:~ franky$] /Users/franky/Downloads/sqlcl/sqlcl/bin/sql /nolog

SQLcl: Release 19.4 Production on Sun Feb 23 07:25:02 2020

Copyright (c) 1982, 2020, Oracle.  All rights reserved.


SQL> set cloudconfig /Users/franky/Downloads/Wallet_TESTDB.zip
Operation is successfully completed.
Operation is successfully completed.
Using temp directory:/var/folders/1h/mwvhtfsn48gc06mh8yp9lgkr0000gn/T/oracle_cloud_config6631314340825709839
SQL> connect admin@testdb_tp
Password? (**********?) *************
Connected.

Let us check the size of the table we are working with:

SQL> select segment_name, segment_type, bytes/1024/1024 mb from user_segments where segment_name='SALES';

SEGMENT_NAME                   SEGMENT_TYPE               MB
------------------------------ ------------------ ----------
SALES                          TABLE                   12096

A 12 GB table might be small comparing to your environment, but is large enough for my test here. It contains 128 million rows:

SQL> select count(*) from sales;

  COUNT(*)
----------
 128000000

The data in it is just for testing purposes:

SQL> select * from sales where rownum<11;

        ID       FLAG PRODUCT                                      CHANNEL_ID    CUST_ID AMOUNT_SOLD ORDER_DA SHIP_DAT
---------- ---------- -------------------------------------------- ---------- ---------- ----------- -------- --------
         1          2 Product Sample for testing query performance          1          1        5000 23/09/92 24/09/92
         2          3 Product Sample for testing query performance          2          2        5000 24/09/92 25/09/92
         3          4 Product Sample for testing query performance          3          3        5000 25/09/92 26/09/92
         4          5 Product Sample for testing query performance          4          4        5000 26/09/92 27/09/92
         5          6 Product Sample for testing query performance          0          5        5000 27/09/92 28/09/92
         6          7 Product Sample for testing query performance          1          6        5000 28/09/92 29/09/92
         7          8 Product Sample for testing query performance          2          7        5000 29/09/92 30/09/92
         8          9 Product Sample for testing query performance          3          8        5000 30/09/92 01/10/92
         9         10 Product Sample for testing query performance          4          9        5000 01/10/92 02/10/92
        10         11 Product Sample for testing query performance          0         10        5000 02/10/92 03/10/92

10 rows selected.

Let us say we want to keep only the current year data which is 2020 in the time of writing. We can check how many rows we have per year by grouping the data set by the order_date column and trunc the result by year:

SQL> select count(*) cnt, extract(year from (trunc(order_date, 'yyyy'))) by_year from sales group by trunc(order_date, 'yyyy')  order by 2;

       CNT    BY_YEAR
---------- ----------
   1292800       1992
   4672000       1993
   4672000       1994
   4672000       1995
   4684800       1996
   4672000       1997
   4672000       1998
   4672000       1999
   4684800       2000
   4672000       2001
   4672000       2002
   4672000       2003
   4684800       2004
   4672000       2005
   4672000       2006
   4672000       2007
   4684800       2008
   4672000       2009
   4672000       2010
   4672000       2011
   4684800       2012
   4672000       2013
   4672000       2014
   4672000       2015
   4684800       2016
   4672000       2017
   4672000       2018
   4672000       2019
    486400       2020

29 rows selected.

If we had this table partitioned by year it would be easy to drop all the partitions older than 2020, but in this case the table is not partitioned.

With a simple command introduced in Oracle Database 12c Release 2 you can accomplish your task easily, online, with no need to rebuild the indexes:

SQL> alter table sales move online including rows where order_date >= '01/01/2020' update indexes;

Table SALES altered.

By executing the same previous query we can confirm that only data from 2020 is kept:

SQL> select count(*) cnt, extract(year from (trunc(order_date, 'yyyy'))) by_year from sales group by trunc(order_date, 'yyyy')  order by 2;

       CNT    BY_YEAR
---------- ----------
    486400       2020

Just one more count to confirm that this is all the data that is left in this table:

SQL> select count(*) from sales;

  COUNT(*)
----------
    486400

If you deleted the rows, despite all the undo data, redo data and archive log files that would be generated, the table would have all the 12GB still allocated, so this is a huge gain by running only one simple command:

SQL> select segment_name, segment_type, bytes/1024/1024 mb from user_segments where segment_name='SALES';

SEGMENT_NAME                   SEGMENT_TYPE               MB
------------------------------ ------------------ ----------
SALES                          TABLE                      47

Bear in mind that to perform any move operation you must have the free space required to accommodate the table that is being moved and all its indexes since Oracle copies the data to temporary tables and indexes in the background. If you are not aware of what I am talking about it is worth to read this one I wrote a couple of years ago: Online Rebuild: What Happens in the Background?

I hope you liked it. Have fun on the next time you have to purge data, but remember to test it in a test environment before going to production! It is also worth to take a look at MOS for bugs and Oracle Docs related to that feature.

See you next time!

Franky

Earlier comments

  1. Sinu

    Hi,

    After executing below command..

    alter table sales move online including rows where order_date >= '01/01/2020' update indexes;

    where the rest data went to ? How can I find that ?

    1. Sinu

      Hi..... I have tried to simulate same but unable to find the object in which rest of data went ?

    2. Franky Weber Faust

      The data was purged >> /dev/null The blog post explains a way to efficiently purge data.

Comments