Question

mysql fastest way to delete a database content on freebsd? please help i've tried to delete from navicat (400,000+ lines), but in a hour... just 100,000 was deleted I haven't phpmyadmin

Was it helpful?

Solution

To delete everything in a table:

TRUNCATE TABLE table_you_want_to_nuke

To delete certain rows, you have two options:

  1. Follow these steps:

    • Create a temporary table using CREATE TABLE the_temp_table LIKE current_table
    • Drop all of the indexes on the temp table.
    • Copy the records you want to keep with INSERT INTO the_temp_table SELECT * FROM current_table WHERE ...
    • TRUNCATE TABLE current_table
    • INSERT INTO current_table SELECT * FROM the_temp_table
    • To speed this option up, you may want to drop all indexes from current_table before the final INSERT INTO, then recreate them after the INSERT. MySQL is much faster at indexing existing data than it is at indexing on the fly.

  2. The option you're currently trying: DELETE FROM your_table WHERE whatever_condition. You probably need to break this into chunks using the WHERE condition or LIMIT, so you can batch it and not bog down the server forever.

Which is better/faster depends on lots of things, mostly the ratio of deleted records to retained records and the number of indexes involved. As always, test this carefully before doing it on a live database, as both DELETE and TRUNCATE will permanently destroy data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top