增加在Django中创建MySQL表的速度? [英] Increase speed for MySQL table creation in Django?
问题描述
我的一些单元测试需要10-15秒的时间才能让mysql创建表。这似乎不必要了。它必须创建大约50个表,但是每秒仍然只有3个表。当运行单元测试时,这是一个很大的烦恼。
作为解决方法,我已经在sqlite3中运行我的单元测试。它是快速的,但我更喜欢运行我的测试MySQL,因为这是我的实时服务器运行。
为了说明速度差异,创建一个新的项目。然后使用mysql运行syncdb。然后尝试使用sqlite3。
[〜/ testproject] ./manage.py syncdb
创建表auth_permission
创建表auth_group
创建表auth_user
创建表auth_message
创建表django_content_type
创建表django_session
创建表django_site
对我来说,在MySQL中创建上述表大约需要2秒钟。 Sqlite3几乎是即时的。
我在我的开发机器上运行mysql。这是我的 my.cnf 。
请建议任何您可以想到的提示或调整可能有助于加快MySQL的表创建时间。
我发现使用sqlite作为替代品,使我的单元测试更快。我也删除了southdb,因为这也减慢了表的创建。
if len(sys.argv)> 1和sys.argv [1] =='test':
/ pre>
DATABASES = {
'default':{
'ENGINE':'django.db.backends.sqlite3',
'NAME':':memory',
'USER':'',
'PASSWORD':'',
'HOST':'',
'PORT ':'',
}
}
INSTALLED_APPS = tuple([x for IN INSTALLED_APPS if x!='south'])
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.
解决方案I have found that using sqlite as a replacement makes my unit tests much faster. I'm also removing southdb, as this slows down table creation too.
if len(sys.argv) > 1 and sys.argv[1] == 'test': DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory', 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', } } INSTALLED_APPS = tuple([x for x in INSTALLED_APPS if x != 'south'])
这篇关于增加在Django中创建MySQL表的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!