views:

265

answers:

1

Some of my unit tests take 10-15 seconds just for mysql to create the tables. This seems unnecessarily long. It has to create around 50 tables, but that's still only 3 tables per second. This is a big annoyance when running unit tests over-and-over.

As a workaround, I have been running my unit tests in sqlite3. It is blazing fast, but I would prefer to run my tests on MySQL since that's what my live servers run.

To illustrate the speed difference, create a fresh project. Then run syncdb on it using mysql. Then try it using sqlite3.

[~/testproject] ./manage.py syncdb
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site

For me, it takes about 2 seconds to create the above tables in MySQL. Sqlite3 is almost instant.

I am running mysql on my development machine. Here is my my.cnf.

Please suggest any tips or tweaks you can think of that might help speed up MySQL's table creation time.

+1  A: 

You can create RAM-disk and move db there, just for unit testing. If you write script for this then it's automatic and very convenient.

Also, for other purposes I've written custom test runner that loads DB from sql dump instead of creating it and then creating tables.

You choose.

Tomasz Zielinski
I've never ran mysql on a ram disk, but I assume it's as simple as setting up a new mysql daemon on a different port, and pointing its data dir to a ram disk. Thanks for the tips.
Gattster
Yes, you could also just switch config (to the one with db path pointing to ram disk) for your existing mysql instance and then restart it - whichever works better for you.
Tomasz Zielinski
Have you actually ran mysql on a ram disk and seen a speed improvement? It seems like it would help but until you test it you really don't know.
Gattster
Yes, I've done it myself and experienced major speed improvement (although I haven't measured times to write exact numbers).
Tomasz Zielinski