I have a fairly simple query that takes a long time – over 10 seconds – in some situations but not others. I’m not sure what’s up, or how to speed it up.
Models:
class Source(models.Model):
feed_url = models.URLField(max_length=512)
# Other fields describing a website with an RSS feed
class Post(models.Model):
source = models.ForeignKey(Source, on_delete=models.CASCADE, related_name="posts")
published_time = models.DateTimeField(db_index=True, null=True)
# Other fields describing a single post on a website
If I do this in the shell:
source = Source.objects.get(pk=123)
first_post = source.posts.order_by("published_time").first()
then for some Sources it happens instantly. But for others, with similar numbers of Posts, it can take 10 or even 20 seconds!
That’s on the live server. But the same query, with the same slow Source, on my dev MacBook – but with a slightly older set of data – also happens instantly.
We’re talking around 1600 Posts for the Source, with a total number of Posts across all Sources of around 520,000.
This is the result of source.posts.order_by("published_time").explain()
:
Sort (cost=5740.17..5744.23 rows=1625 width=700)
Sort Key: published_time
-> Bitmap Heap Scan on feeds_post (cost=25.02..5653.51 rows=1625 width=700)
Recheck Cond: (source_id = '123'::smallint)
-> Bitmap Index Scan on feeds_post_source_id_3dffeb3b (cost=0.00..24.61 rows=1625 width=0)
Index Cond: (source_id = '123'::smallint)
I’m a bit stumped where to look next for what’s causing such a slowdown for a simple query, but only in some cases.