Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to clear the content out of many tables

Right now we're using TRUNCATE to clear out the contents of 798 tables in postgres (isolated test runs). Where possible we use transactions. However, in places where it's not possible, we'd like the fastest way to reset the state of the DB.

We're working towards only actually calling truncate on the tables that have been modified (for any given test only a few of the 798 tables will be modified).

What is the fastest way to delete all of the data from many PostgreSQL tables?

like image 380
Alex Gaynor Avatar asked Jan 21 '26 08:01

Alex Gaynor


1 Answers

Two things come to mind:

  1. Setup the clean DB as a template and createdb a copy from it before each test.
  2. Setup the clean DB as the default schema, but run the TransactionTests in a different schema (SET search_path TO %s).
like image 105
coderanger Avatar answered Jan 23 '26 11:01

coderanger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!