{
  "id": 6561811,
  "title": "count() on a prefetched relation is free. filter() costs a query per row.",
  "url": "https://urgent.news/2026/09/10/count-on-a-prefetched-relation-is-free-filter-costs-a-query-per-row",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-10T08:31:48.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/syrian963/count-on-a-prefetched-relation-is-free-filter-costs-a-query-per-row-4acm"
  },
  "original_language": "en",
  "account": "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.\n\nFor 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.\n\nTo 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.\n\nIn 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.\n\nHowever, 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.",
  "summary": "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 .…",
  "key_points": [
    "Prefetching related objects can optimize queries by loading data in a single query.",
    "Using .filter() on a prefetched relation can lead to additional queries per row.",
    "Moving the condition into the prefetch can reduce queries to just two."
  ],
  "editors_take": "Using .filter() on a prefetched relation in Django incurs additional queries, but moving the filter condition into the prefetch itself or performing operations directly on prefetched data can avoid extra queries.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}