[Django]-MySQL database being hit too many times with django query

10👍

You can decide between several solutions:

  1. Generic relation pre-fetching, you can use this snippet like this example app.

  2. Identity map/caching, use the content type and object id to make the key to cache the model, here is an example you can take the code inside __getstate__ and __setstate__ and make a template tag, this is the fastest solution you can find

  3. Fetching relations directly, at some point you will probably need one of the solutions detailed in this article about generic relation performance later when you work with foreign keys.

  4. Use JonhyCache, JohnyCache is a caching framework for querysets which works with the django caching abstraction. I did not personally try it yet, as the three solutions were sufficient in my projects.

  5. Cache the template fragment, less hacky, it looks like official way to deal with this kind of issue that spawns a lot of queries, and when the user changes his favorites you could invalidate the cached version.

Note that select_related() doesn’t work on generic relations. This is because the related object can be in any table so it’s impossible to make proper joins in pure sql. Generic relations are a feature from Django, not from MySQL.

👤jpic

2👍

Try using the select_related queryset method.

Something like this should do the trick:

item = Favorite.objects.favorites_for_user(user=request.user).select_related('content_type', 'content_object')

EDIT: jpic has rightly pointed out that select_related won’t work for ‘content_object’ because it’s a generic foreign key. Unfortunately there’s no nice built in way to handle this, and there won’t be until Django 1.4 rolls around. In the meantime, check out this SO question for some pointers. jpic has also posted some good solutions in his answer.

Leave a comment