We are in the process of moving a Django 1.3.1 project from a Mac OS X 10.5 server to a Red Hat Enterprise Server 6.0 VM. For most things, the new server is about 2 times faster than the old one.
For Django queries, the new server is much slower. A single objects.get that should take less than a second takes over two seconds. I've tracked it down to this line in one of my models.py:
That line is not part of any model; it is run whenever Django is started up for use in one of my fields:
server = models.ForeignKey(Host, default=defaultHost)
That is the only place defaultHost is used. If I change the defaultHost line to:
defaultHost = None
model.objects.get requests drop from 2-4 seconds to less than half a second. The change makes no difference on the current (Mac OS X) server.
Any idea what could be causing this or how I can find out? On the front end, things that take a second or two to display now take a minute, two minutes, or more.
This is the test script I'm using:
#!/usr/bin/python
import datetime
import sys, os
baseDir = '/projects'
sys.path.append(baseDir)
project_name = 'usdpages'
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % project_name
from usdpages.pages.models import Page
start = datetime.datetime.now()
Page.objects.get(slug='vector-apps')
end = datetime.datetime.now()
print 'Duration:', end - start
With startup query:
$ ./test
Duration: 0:00:02.252040
$ ./test
Duration: 0:00:02.298540
$ ./test
Duration: 0:00:01.851589
Without startup query:
$ ./test
Duration: 0:00:00.140946
$ ./test
Duration: 0:00:00.142173
$ ./test
Duration: 0:00:00.140059
I also tested it with the 1.4c2 release candidate, and it is slow regardless of that defaultHost line; it takes 1.8-2.4 seconds for that one Page query with a defaultHost=None, and 2.9 seconds consistently with defaultHost set to a query result.
One odd thing: in the method _cursor in the class DatabaseWrapper in /usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py if I change:
if not self._valid_connection():
to:
if True or not self._valid_connection():
the query time drops to the .14 second range as well. (In 1.4c2 it drops to .77-.81 seconds.)