Reviews
Loading 20 records. Compare query count and execution time between the two approaches.
Problem
The serializer accesses review.user.email and review.book.title. Both user and book are ForeignKeys — without optimization, each review fires 2 extra queries. Result: 2N+1 queries.
Review.objects.all()
Fix
select_related('user', 'book') performs SQL JOINs across reviews, users, and books in a single query. Accessing review.user or review.book costs zero extra queries.
Review.objects.select_related('user', 'book')
Unoptimized
41 queries
35.5 ms
N+1 detected: 41 queries for 20 rows
| User | Book | Rating | Spoiler? | Date |
|---|---|---|---|---|
| [email protected] | It is also a garbage-collected runtime system. | ★★★★★ | False | Jul 21, 2026 |
| [email protected] | Do you have any idea why this is not working? | ★★★★ | False | Jul 21, 2026 |
| [email protected] | Atoms are used within a program to denote distinguished values. | ★★ | False | Jul 21, 2026 |
| [email protected] | Tuples are containers for a fixed number of Erlang data types. | ★★ | False | Jul 21, 2026 |
| [email protected] | Ports are used to communicate with the external world. | ★★★★ | True | Jul 21, 2026 |
| [email protected] | Atoms are used within a program to denote distinguished values. | ★★★★ | False | Jul 21, 2026 |
| [email protected] | The arguments can be primitive data types or compound data types. | ★★★ | False | Jul 21, 2026 |
| [email protected] | The syntax {D1,D2,...,Dn} denotes a tuple whose arguments are D1, D2, ... Dn. | ★★★★★ | False | Jul 21, 2026 |
| [email protected] | The arguments can be primitive data types or compound data types. | ★★★ | False | Jul 21, 2026 |
| [email protected] | The syntax {D1,D2,...,Dn} denotes a tuple whose arguments are D1, D2, ... Dn. | ★★★★★ | False | Jul 21, 2026 |
| [email protected] | Its main implementation is the Glasgow Haskell Compiler. | ★★ | False | Jul 21, 2026 |
| [email protected] | Where are my pants? | ★★ | False | Jul 21, 2026 |
| [email protected] | He looked inquisitively at his keyboard and wrote another sentence. | ★ | False | Jul 21, 2026 |
| [email protected] | It is also a garbage-collected runtime system. | ★★ | True | Jul 21, 2026 |
| [email protected] | Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. | ★★★★★ | False | Jul 21, 2026 |
| [email protected] | I don't even care. | ★★★★★ | False | Jul 21, 2026 |
| [email protected] | Initially composing light-hearted and irreverent works, he also wrote serious, sombre and religious pieces beginning in the 1930s. | ★★★ | False | Jul 21, 2026 |
| [email protected] | I don't even care. | ★★★★ | False | Jul 21, 2026 |
| [email protected] | The sequential subset of Erlang supports eager evaluation, single assignment, and dynamic typing. | ★★★ | False | Jul 21, 2026 |
| [email protected] | Ports are created with the built-in function open_port. | ★★★★★ | False | Jul 21, 2026 |
Optimized
1 queries
20.1 ms
41 queries reduced to 1
(35.5ms to 20.1ms)
| User | Book | Rating | Spoiler? | Date |
|---|---|---|---|---|
| [email protected] | It is also a garbage-collected runtime system. | ★★★★★ | False | Jul 21, 2026 |
| [email protected] | Do you have any idea why this is not working? | ★★★★ | False | Jul 21, 2026 |
| [email protected] | Atoms are used within a program to denote distinguished values. | ★★ | False | Jul 21, 2026 |
| [email protected] | Tuples are containers for a fixed number of Erlang data types. | ★★ | False | Jul 21, 2026 |
| [email protected] | Ports are used to communicate with the external world. | ★★★★ | True | Jul 21, 2026 |
| [email protected] | Atoms are used within a program to denote distinguished values. | ★★★★ | False | Jul 21, 2026 |
| [email protected] | The arguments can be primitive data types or compound data types. | ★★★ | False | Jul 21, 2026 |
| [email protected] | The syntax {D1,D2,...,Dn} denotes a tuple whose arguments are D1, D2, ... Dn. | ★★★★★ | False | Jul 21, 2026 |
| [email protected] | The arguments can be primitive data types or compound data types. | ★★★ | False | Jul 21, 2026 |
| [email protected] | The syntax {D1,D2,...,Dn} denotes a tuple whose arguments are D1, D2, ... Dn. | ★★★★★ | False | Jul 21, 2026 |
| [email protected] | Its main implementation is the Glasgow Haskell Compiler. | ★★ | False | Jul 21, 2026 |
| [email protected] | Where are my pants? | ★★ | False | Jul 21, 2026 |
| [email protected] | He looked inquisitively at his keyboard and wrote another sentence. | ★ | False | Jul 21, 2026 |
| [email protected] | It is also a garbage-collected runtime system. | ★★ | True | Jul 21, 2026 |
| [email protected] | Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. | ★★★★★ | False | Jul 21, 2026 |
| [email protected] | I don't even care. | ★★★★★ | False | Jul 21, 2026 |
| [email protected] | Initially composing light-hearted and irreverent works, he also wrote serious, sombre and religious pieces beginning in the 1930s. | ★★★ | False | Jul 21, 2026 |
| [email protected] | I don't even care. | ★★★★ | False | Jul 21, 2026 |
| [email protected] | The sequential subset of Erlang supports eager evaluation, single assignment, and dynamic typing. | ★★★ | False | Jul 21, 2026 |
| [email protected] | Ports are created with the built-in function open_port. | ★★★★★ | False | Jul 21, 2026 |