Faster alternative to MySQL Delete Table Contents / Truncate

0 min read 151 words

From time to time you might have some rather big tables that you want to delete all the data quickly and start afresh.

You have a few options at this point. The first you?ll probably look at is:

DELETE FROM `table_name`

The benefit of doing this is DELETE provides the ability to Rollback if all hell breaks loose, but it also means that it will take longer because it requires more memory to store all this additional data

Options 2 is to just truncate the table:

TRUNCATE `table_name`

This is the quicker of the 2 options so far and will merely delete all rows as is

Option 3 is to clone the table structure and then rename tables:

CREATE TABLE table_name2 LIKE table_name;<br> RENAME TABLE table_name TO table_name3;<br> RENAME TABLE table_name2 TO table_name;

This last option is the quickest and can be performed on very large tables with lightning speed results

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags