Table of contents
For situations where the amount of data is relatively large, it is best to split tables, but splitting tables comes at a cost. After splitting tables, the program must be adjusted accordingly, which requires that this issue be taken into consideration in early planning. A more common approach is to use more partitions (personal opinion) and transfer old data at the same time. For example, you can archive login logs, inventory information, etc. data from 5 years ago and put it into another table to keep the tables in use. A certain amount of data (such as 10 million, 5 million). Then after the archiving is completed, the original table data must be deleted. At this time, the amount of data deleted may be relatively large (5 million, 10 million). So what are the methods to delete large amounts of data? This article takes the oracle database as an example to illustrate various methods.
1. truncate command
If you no longer need the table data, you can use this command. This command cannot be restored once used, so use it with caution! ! !
truncate table test;
2. Temporary table
Transfer the data in the table to a temporary table, use the truncate command on the original table, and then import the data into the original table.
Disadvantages: cannot be used online
3. nologging mode
For specific usage, please check this article Oracle Nologging Usage (Reposted) - A Drop in the Sea - Blog Park
create table test_bak nologging as select * from test;
4、parallel
delete /*+ parallel(并行度) */ test where ...;
The degree of parallelism can be specified as the number of cores of the CPU.
Disadvantages: May kill the database
5. Divide into parts
This method is to delete less data at a time, for example, only delete one month at a time, which is a stupid method.
DECLARE
v_nums INTEGER := 24;
v_start VARCHAR2(10);
v_startdate DATE;
v_enddate DATE;
v_1 INTEGER;
BEGIN
v_start := '2019-01-01'; --开始日期
FOR v_1 IN 1 .. v_nums
LOOP
v_startdate := add_months(to_date(v_start, 'yyyy-mm-dd'), v_1 - 1);
v_enddate := add_months(v_startdate, 1);
dbms_output.put_line(v_1 || '-' || v_startdate || '-' || v_enddate);
DELETE FROM dd_goods_inventory_bak t
WHERE t.querydate < v_enddate
AND t.querydate >= v_startdate;
COMMIT;
END LOOP;
END;