MySQL emptying a table

Just a quick MySQL optimization note. Many times we're required to clear a database table and reset the auto increment counter. This is common with import scripts (importing data from Excel, etc).

The uncool way to do it:

DELETE FROM myTable
ALTER TABLE myTable AUTO_INCREMENT = 1

The cool (and more efficient) way to do it:

TRUNCATE TABLE myTable

 

Noting that the TRUNCATE statement automatically resets the counter.

Why follow me on Twitter?

  • I tweet about new technologies, services or libraries I find interesting
  • Yeah, sometimes I'll post a pet-peeve or rant about something trivial
  • If I discover something that made my web development life easier, I share it
  • I'll shout out any handy tip that I think might be useful to other devs


Tagged .

Updated: 2011-09-01

Phil LaNasa follow us in feedly