Professional Documents
Culture Documents
Oracle Best Fastest Way To Delete Data From Large Table Tips
Oracle Best Fastest Way To Delete Data From Large Table Tips
Oracle Best Fastest Way To Delete Data From Large Table Tips
��
Search BC Oracle Sites Oracle Best Fastest Way to Delete Data from
Custom Search
Large Table Tips
Search
Oracle Database Tips by Donald BurlesonOctober 25, 2015
Home
E-mail Us
Oracle Articles
New Oracle Articles
Question: I have a very large table and I need to delete millions of rows from the
table without the table fragmenting. I also need to use the best delete method, a
best-practice way to delete large amounts of rows as fast as possible.
Oracle Training What are the options for deleting large amounts of rows from large tables?
Oracle Tips
Answer: The answer to the best way to delete rows from an Oracle table is: It
Oracle Forum
depends! In a perfect world where you can take the table offline for maintenance,
Class Catalog
a complete reorganization is always best because it does the delete and places the
table back into a pristine state. We will address the tools for doing large scale
deletes and the appropriate methods for each environment.
Remote DBA
Oracle Tuning Also see deleting large numbers of rows quickly.
Emergency 911
Factors and tools for massive deletes
RAC Support
Apps Support The choice of the delete methods depends on many factors:
Analysis
Design Is the target table partitioned? Partitioning greatly improves delete
Implementation performance. For example, it is common to have a large time-based table
Oracle Support
partition and deleting elderly rows from these table can be as simple as
dropping the desired partition. See these notes on managing partitioned
tables.
SQL Tuning Can you reorganize the table after the delete to remove fragmentation?
Security
What percentage of the table will be deleted? In cases where you are
Oracle UNIX
deleting more than 30-50% of the rows in a very large table it is faster to
Oracle Linux
use CTAS to delete from a table than to do a vanilla delete and a
Monitoring
reorganization of the table blocks and a rebuild of the constraints and
Remote support indexes.
Remote plans
Remote services Do you want to release the space consumed by the deleted rows? If you
Application Server know that the empty space will be re-used by subsequent DML then you
Applications
will want to leave the empty space within the table. Conversely, if you
want to released the space back onto the tablespace then you will need to
Oracle Forms
reorganize the table.
Oracle Portal
App Upgrades There are many tools that you can use to delete from large tables:
SQL Server
Oracle Concepts dbms_metadata.get_ddl: This procedure wil punch-off the definitions of
Software Support all table indexes and constraints.
Remote Support
dbms_redefinition: This procedure will reorganize a table while it remains
Development
available for updating.
Implementation
Create Table as Select: You can use CTAS to copy a table while
removing rows in bulk.
Consulting Staff
www.dba-oracle.com/t_oracle_fastest_delete_from_large_table.htm 1/5
1/10/2019 Oracle Best Fastest Way to Delete Data from Large Table Tips
Consulting Prices Rename table: If you copy a table when deleting rows you can rename it
Help Wanted! back to its original name.
COMMIT: In cases where a delete might run for many hours, even the
largest UNDO log will not be able to hold the rollback information and it
becomes necessary to do the delete in a PL/SQL loop, issuing a COMMIT
every zillion-rows to free-up the undo logs. This approach will be re-
Oracle Posters startable automatically because the delete will pick-up where it left off as on
Oracle Books your last commit checkpoint.
Oracle Scripts
Ion If you must do the delete in a 24x7 environment you have limited methods for
Excel-DB
deleting the rows:
Online table redefinition: You can drop large numbers of rows from a
table by adding a WHERE clause predicate to filter-out unwanted rows
when you copy the table.
If you have a scheduled maintenance downtime window and you are able to take a
consistent backup of the table immediately before the delete, you can use several
methods:
-- pre 9i:
set transaction use rollback_segment = 'HUGE_RBS';
OPTION TWO: Delete into a new tablespace. Note that it is common for
super-large tables to reside within their own tablespace for ease of
management:
STEP 2 - Copy the table using a WHERE clause to delete the rows:
create table
new_mytab
as
select * from mytab where year = '2012'
tablespace
new_tablespace;
www.dba-oracle.com/t_oracle_fastest_delete_from_large_table.htm 2/5
1/10/2019 Oracle Best Fastest Way to Delete Data from Large Table Tips
rename mytab to old_mytab;
rename new_mytab to mytab
OPTION THREE: Delete and copy back into an existing tablespace. Note
that this will take longer then option one because the indexes and
constraints will be update for each and every row copied
STEP 1 - Copy the table using a WHERE clause to delete the rows:
create table
new_mytab
as
select * from mytab where year = '2012'
tablespace new_tablespace;
truncate mytab;
rename new_mytab to mytab
STEP 3 - Copy-back the rows into the original table definition. Please note
that this step may required a dedicated rollback segment:
insert into
mytab
as
select * from new_mytab;
In sum, a create table as select (CTAS) approach can be faster than a vanilla delete
when the majority of the table rows are being deleted. CTAS is fast because
CTAS can be parallelized, and the required full-scan can be run with parallel read
processes such that on a 64 CPU server, the CTAS will scan the table 63 times
faster. CTAS can be used with the NOLOGGING option.
www.dba-oracle.com/t_oracle_fastest_delete_from_large_table.htm 3/5
1/10/2019 Oracle Best Fastest Way to Delete Data from Large Table Tips
Note: This Oracle documentation was created as a support and Oracle training
reference for use by our DBA performance tuning consulting professionals. Feel
free to ask questions on our Oracle forum.
Burleson Consulting
www.dba-oracle.com/t_oracle_fastest_delete_from_large_table.htm 4/5
1/10/2019 Oracle Best Fastest Way to Delete Data from Large Table Tips
www.dba-oracle.com/t_oracle_fastest_delete_from_large_table.htm 5/5