Skip to content

Instantly share code, notes, and snippets.

@jdaily
Created April 3, 2014 03:00
Show Gist options
  • Save jdaily/9947545 to your computer and use it in GitHub Desktop.
Save jdaily/9947545 to your computer and use it in GitHub Desktop.
Empty a mysql table FAST
TRUNCATE is usually a fast operation (much faster than DELETE FROM). But sometimes it just hangs;
TRUNCATE on tables with no FOREIGN KEYs should act fast: it translate to dropping the table and creating a new one (and it all depends on the MySQL version, see the manual).
Renaming a table (without Foreign Keys) is much faster if its large enough. Instead of:
```
TRUNCATE log_table
```
Do:
```
CREATE TABLE log_table_new LIKE log_table;
RENAME TABLE log_table TO log_table_old, log_table_new TO log_table;
DROP TABLE log_table_old;
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment