Search code examples
mysqldatabasedatabase-performancedata-import

How to properly wipe a database, and re-import?


I am unsure about the best way to do this. As I'm getting ready to put a new database into production, I need to import data from the old database that has been formed in the meantime of me working on it. The new database now also contains a lot of fake data that was used for testing, which I have to get rid of, so a fresh complete re-import seems reasonable.

Now, truncating all the tables in the new database cannot go through, because the foreign keys prevent it. Simply deleting the data instead would solve that problem, but it leaves the AUTO_INCREMENT indexes to the values where they were, so it's not a "proper" wipe. Now, there could be more properties such as that one, that would be left over (so to say), but this is the only one that I'm aware of.

So my question now is, how much of a problem could these "leftover" pieces of data pose to performance, if I were to go with the simple DELETE solution?

And also; is there a way that would be more thorough in cleaning it out, and also allow me to, of course, keep the defined constraints?


Solution

  • First i would use some gui tool to create the dump for the old DB ( like mySql workbench, or what ever you prefer ). Check options "Export to self-contained file", and check "Dump stored procedures and functions","Dump events" and "Dump triggers".

    Then get create scripts for all tables not included in the old DB. You can do this via "reverse engineer" option. If you have trouble with this part this post will help. How to get a table creation script in MySQL Workbench?

    When you have old DB dump and create scripts for new sql tables, combine them to a single sql file.

    On the first row add:

    SET FOREIGN_KEY_CHECKS = 0;
    

    On the last row add:

    SET FOREIGN_KEY_CHECKS = 1;
    

    Run the script. As a result you should have all tables ( new without data and old with data ), with all relations set properly. Hope it will work for you.