Reputation: 86845
I'm using Spring-data-jpa
with CrudRepository
to persist data in a postgres
db.
On a daily basis I want to renew the full db content, which means detele all data in a specific table first.
The repository offers a method dao.deleteAll()
, but that lasts very long as I have about 500k entries.
How can I improve this?
Upvotes: 3
Views: 9690
Reputation: 96
You can execute native sql query on entity manager, and truncate your database
String sql = "TRUNCATE tbl_name;";
entityManager.createNativeQuery(sql).executeUpdate();
Upvotes: 7
Reputation: 32296
The TRUNCATE
command is much faster on large tables because it simply nukes the files at the file system level, whereas DELETE
marks each record as free without reducing file size, with any triggers defined on them being run.
Upvotes: 1