I encountered this problem recently for a Django ticket for speeding up part of Django’s test runner.
We have a list of tables to truncate.
How can we figure out which are already empty, so we can skip issuing a somewhat costly
TRUNCATE TABLE on them?
You might think the database could skip
TRUNCATE TABLE on empty tables, but it normally does work beyond removing the rows such as resetting auto-increment counters.
TRUNCATE TABLE is even equivalent to
DROP TABLE and
CREATE TABLE, which requires removing and creating the table file on disk.