I'm building a Django app with some pretty straightforward queries. When I run it locally the performance is pretty poor: DebugToolbar reports 4119.00 ms (6 queries) which is obviously pretty bad. The longest-running query is this one:
Django views.py code:
reviews = list(Review.objects.select_related('band', 'record', 'label').order_by('-date_posted')[:12])
Outputted SQL:
SELECT * FROM `reviews`
INNER JOIN `bands` ON (`reviews`.`band_id` = `bands`.`id`)
INNER JOIN `records` ON (`reviews`.`record_id` = `records`.`id`)
INNER JOIN `label` ON (`reviews`.`label_id` = `label`.`id`)
ORDER BY `reviews`.`date_posted` DESC
LIMIT 12
Now obviously there's a few joins there, but my tables are properly indexed. When I run that query in phpMyAdmin I get Query took 0.0241 sec, but in DebugToolbar I see 1838.00 ms. The other 5 queries are similarly sluggish, but run normally when queried directly.
I'm running Django on Windows and using MySQL (WAMP server). Can anyone think of any reason why Django's own queries could be so slow?
Make sure you are using the same database to do the comparisons. If one has a small amount of data, and the other is huge, the timings will change. This sounds like a silly thing to point out, but simpler mistakes have been made.
In Django, set DEBUG=False, so that Django won't store and trace the queries, which could add overhead. I wouldn't expect it to add as much as you're seeing, but you need to make the environment realistic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With