I had a query that works fine for about 2 years. The database table has about 50 million rows and is growing slowly. Last week, one of my requests went from an almost instant return to hours of work.
Rank.objects.filter(site=Site.objects.get(profile__client=client, profile__is_active=False)).latest('id')
I narrowed down the slow query to the Rank model. This seems to be related to using the latest () method. If I just ask for a request, it will immediately return an empty request.
#count returns 0 and is fast Rank.objects.filter(site=Site.objects.get(profile__client=client, profile__is_active=False)).count() == 0 Rank.objects.filter(site=Site.objects.get(profile__client=client, profile__is_active=False)) == []
Below are the results of running EXPLAIN. http://explain.depesz.com/s/wPh
AND EXPLAIN ANALYZE: http://explain.depesz.com/s/ggi
I tried to vacuum the table, no change. There is already an index (ForeignKey) in the "site" field.
Itβs strange if I run the same request for another client that already has Rank objects associated with her account, then the request returns very quickly again. It seems that this is only a problem when they do not have Rank objects for this client.
Any ideas?
Version: Postgres 9.1, Django 1.4 svn trunk rev 17047
erikcw
source share