Urgent.News

What's breaking now, across thousands of outlets.

Tech

count() on a prefetched relation is free. filter() costs a query per row.

Twelve queries where you expected two, in a loop that looks like somebody already optimised it. Second of two posts from the same afternoon. The first, Django's .exclude() does not drop your NULL rows , is about a check I measured and did not build. This is the one that survived. Here is a page that is slower than the version with no optimisation in it at all: orders = Order . objects .…

Prefetching related objects in Django can be a double-edged sword. On one hand, it can optimize queries by loading related data in a single query. On the other hand, using methods like .filter() on a prefetched relation can lead to additional queries, potentially slowing down the application.

For example, if you prefetch order lines and then use .filter(active=True) on them, Django cannot fulfill this condition from the cached prefetch. Instead, it will issue a new query for each parent order, resulting in a total of twelve queries for ten orders. This is because .filter() cannot be answered from the cache and requires a fresh query against the database.

To avoid this, you can move the condition into the prefetch itself. By specifying a queryset with the desired filter condition, you can reduce the number of queries to just two, regardless of the number of orders. This is done by creating a Prefetch object with the desired queryset and assigning it to an attribute of the prefetched relation.

In cases where you need to perform more complex operations on the prefetched relation, such as ordering or selecting a single object, you may not need a Prefetch at all. If the rows are already in memory, you can perform these operations directly without making additional queries to the database.

However, if you need a different filter condition for each iteration of the loop, it may be more efficient to delete the prefetch entirely. Prefetching in these scenarios often leads to unnecessary queries and extra memory usage. It's important to carefully evaluate whether the prefetch is providing any benefits before implementing it.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Thursday 10 September →