Hi,
I already checked and on all the tables that uses the id col of the main table as a foreign key have index on that column.
I tried all the next 4 solutions :
1)delete from my_table where end_date <= to_date('12/12/2018','DD/MM/YYYY') and end_date > to_date('11/12/2018','DD/MM/YYYY');
Execution time: 350603.047 ms ~ 5.8 minutes
2)DELETE FROM my_table WHERE id IN (select id from my_table where end_date <= to_date('12/12/2018','DD/MM/YYYY') and end_date > to_date('11/12/2018','DD/MM/YYYY'));
Execution time: 333166.233 ms ~ 5.5 minutes
3) set temp_buffers='1GB';
SET
create temp table id_temp as select id from my_Table where end_date <= to_date('12/12/2018','DD/MM/YYYY') and end_date > to_date('11/12/2018','DD/MM/YYYY') ;
SELECT 1572864
Time: 2196.670 ms
Execution time: 459650.621 ms 7.6minutes
4)delete in chunks :
do $$
declare
rec integer;
begin
select count(*) from my_table into rec where end_date <= to_date('12/12/2018','DD/MM/YYYY') and end_date > to_date('11/12/2018','DD/MM/YYYY');
while rec > 0 loop
DELETE FROM my_Table WHERE id IN (select id from my_tablewhere end_date <= to_date('12/12/2018','DD/MM/YYYY') and end_date > to_date('11/12/2018','DD/MM/YYYY') limit 5000);
rec := rec - 5000;
raise notice '5000 records were deleted, current rows :%',rec;
end loop;
end;
$$
;
Execution time : 6 minutes.
So, it seems that the second solution is the fastest one. It there a reason why the delete chunks (solution 4) wasnt faster?