Database: PostgreSQL RDS on AWS AWS EC2 Instance with 8GB RAM
I have a scheduled task in Java Spring that runs every midnight 00:00:01. Which fetches the data older than currentDateTime minute 15 days. (mostly 1.5+M rows everyday)
The data is than written in a CSV, The CSV is then Zipped, and the ZIP is then uploaded to S3.
All the data that was written in CSV is then DELETED from the table using Native query.
DELETE FROM MY_TABLE WHERE id>='1' AND id<='10000000';
The ID range is obtained from the LIST at Step 1.
Entire process up until uploading to S3 is completed in almost 1.5 minutes. But DELETE query often crashes the EC2 instance.
Please note:
I have indexes on the ID table
Entire process has to be automated, so it has to be done through code scheduler only.
Dumping data to temp_table is not an option, as this data is useless
Tried solutions given [here][1]
The delete method is annotated with
@Async
[1]: https://stackoverflow.com/a/72725343@Async private void deleteLog(Long startId, Long lastId){ deviceAppStatusLogRepository.deleteBetween(startId, lastId); }