membersound
membersound

Reputation: 86845

How to quickly drop a database table content using Spring?

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

Answers (2)

evilkyro
evilkyro

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

Patrick
Patrick

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

Related Questions