تخطَّ إلى المحتوى

N Plus One Query (ARCH2050)

هذا المحتوى غير متوفر بلغتك بعد.

Key python.orm.n-plus-one-query · Severity warning · Category orm

Opening a new query for related data on every loop iteration produces N+1 round-trips for N rows. It is the single biggest ORM performance killer.

authors = Author.objects.all()
for author in authors:
print(author.profile.bio) # a separate SELECT per iteration
authors = Author.objects.select_related("profile")
for author in authors:
print(author.profile.bio) # loaded in one JOIN

Eager-load related data with select_related / prefetch_related instead of querying inside the loop.