#31276: Add 'flush' option to only clear non-empty tables -------------------------------------+------------------------------------- Reporter: Adam | Owner: nobody (Chainz) Johnson | Type: | Status: new Cleanup/optimization | Component: Database | Version: master layer (models, ORM) | Severity: Normal | Keywords: Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- This was suggested as an extra feature for Django-MySQL by @jonatron in https://github.com/adamchainz/django-mysql/pull/611/files , but it would make more sense to make it in Django core.
The `sql_flush` database operation, used by the 'flush' management command and, thus through `TransactionTestCase`, creates one `TRUNCATE TABLE` statement for every table - regardless of whether they are empty already. This means unnecessary database operations are run, which will be most noticeable in test runs. Instead of flushing *every* table, we can flush only the non-empty ones. A minimal, hopefully cross-database way of quickly finding out which tables in a list are non-empty is to make existence queries on them combined with union: {{{ chainz@localhost [14]> (select 't' as has_rows from t limit 1) union (select 't2' as has_rows from t2 limit 1); +----------+ | has_rows | +----------+ | t2 | +----------+ 1 row in set (0.001 sec) }}} We could use logic like this to batch the tables into such union queries in `django.core.management.sql.sql_flush`. We could then reduce the list of tables we wish to flush down to the non-empty ones for the `sql_flush` operation. Perhaps it would be best to put this behind a flag that is set only in tests, for backwards compatibility. -- Ticket URL: <https://code.djangoproject.com/ticket/31276> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/053.66fdaded95bcfdee116af8e7a85b8eb1%40djangoproject.com.