strawberry-graphql/strawberry-django

N+1 queries due to queryset.count() if cache is empty

開放

#788 建立於 2025年9月30日

 (1 則留言) (0 個反應) (0 位負責人)Python (152 個分叉)auto 404
enhancementhelp wanted

倉庫指標

星標
 (496 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

Describe the Bug

In a nested ListConnectionWithTotalCount, if the child has total_count in the query, if there is a parent node with no related child nodes, there is a queryset.count() triggered. This leads to N+1 queries like scenario if very few parent nodes have child nodes.

https://github.com/strawberry-graphql/strawberry-django/blob/269954c582dfa38dc93af95abd2a275888f50103/strawberry_django/pagination.py#L269-L298

Instead, if the queryset is optimized (is_optimized_by_prefetching is True) and there is no offset / limit mentioned then it should just return 0 assuming no nodes were found, calling the count() method only for unoptimized querysets.

貢獻者指南