Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What is the Django ORM, and what is the N+1 query problem?

The ORM maps Python classes to database tables so you write Book.objects.filter(author__name='X') instead of SQL. QuerySets are lazy — nothing hits the database until you iterate, slice, or call something like len() — and they are cached once evaluated.

The N+1 problem is the most common Django performance bug. This code runs one query for the books and then one more per book:

for book in Book.objects.all():      # 1 query
    print(book.author.name)          # 1 query each time

A hundred books means a hundred and one queries.

The two fixes:

  • select_related('author') for forward ForeignKey and OneToOne relations. It performs a SQL JOIN and returns everything in one query.
  • prefetch_related('tags') for ManyToMany and reverse ForeignKey relations. It runs a second query and joins the results in Python, because a JOIN would multiply rows.

Note: Say how you would find it, not just how to fix it. django-debug-toolbar shows the query count per page, and assertNumQueries in a test stops it coming back.

All Django interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as