Books
Loading 20 records. Compare query count and execution time between the two approaches.
Problem
publisher is a ForeignKey — each book fires a separate query to fetch it. book_authors, genres, and authors.count() all fire per-row queries too. Result: 4N+1 queries.
Book.objects.all()
Fix
select_related('publisher') JOINs the publisher table into the main query. prefetch_related('book_authors__author', 'genres') fetches all related rows in 2 extra queries. annotate(author_count=Count('authors')) computes the count in SQL. Result: 3 queries total.
Book.objects.select_related('publisher').prefetch_related(
Prefetch('book_authors', queryset=BookAuthor.objects.select_related('author')),
'genres',
).annotate(author_count=Count('authors'))
Unoptimized
107 queries
71.9 ms
N+1 detected: 107 queries for 20 rows
| Title | Authors | Publisher | Genres | Pages | # Authors |
|---|---|---|---|---|---|
| Any element of a tuple can be accessed in constant time. | Sol Mullen (Primary Author) | Bennett Books | Cooking, Science Fiction, Travel | 601 | 1 |
| Any element of a tuple can be accessed in constant time. | Marty Lamb (Primary Author) | Phillips Publishing | Science Fiction | 155 | 1 |
| Any element of a tuple can be accessed in constant time. | Dion Cotton (Primary Author), Adelle Mejia (Illustrator) | Snider House | Fiction, Travel | 936 | 2 |
| Any element of a tuple can be accessed in constant time. | Milton Dotson (Primary Author) | Haynes Books | Non-Fiction | 421 | 1 |
| Any element of a tuple can be accessed in constant time. | Victor Hinton (Primary Author) | Hinton House | Children's, Cooking | 513 | 1 |
| Any element of a tuple can be accessed in constant time. | Armando Ferrell (Primary Author), Maye Cantrell (Co-Author) | Coleman House | Children's | 258 | 2 |
| Any element of a tuple can be accessed in constant time. | Emil Gay (Primary Author), Magali Vazquez (Illustrator) | Lawson House | Horror | 570 | 2 |
| Any element of a tuple can be accessed in constant time. | Gertrud Gentry (Primary Author) | Mcmillan House | Fantasy | 698 | 1 |
| Any element of a tuple can be accessed in constant time. | Oswaldo Cline (Primary Author), Debbra Daugherty (Co-Author) | Moran Press | Biography, Fantasy, Fiction | 404 | 2 |
| Any element of a tuple can be accessed in constant time. | Alfredo Joyce (Primary Author) | Barrera Publishing | Fiction | 495 | 1 |
| Any element of a tuple can be accessed in constant time. | Roni Castro (Primary Author) | Newman Media | Horror, Mystery | 747 | 1 |
| Any element of a tuple can be accessed in constant time. | Jess Peterson (Primary Author) | Snyder Publishing | Self-Help | 347 | 1 |
| Any element of a tuple can be accessed in constant time. | Karly Francis (Primary Author) | Chapman House | Self-Help, Travel | 405 | 1 |
| Any element of a tuple can be accessed in constant time. | Xochitl Nixon (Primary Author) | Fitzgerald House | Children's, Poetry | 704 | 1 |
| Any element of a tuple can be accessed in constant time. | Brain Molina (Primary Author), Tashina May (Co-Author) | Diaz Media | Science | 1188 | 2 |
| Any element of a tuple can be accessed in constant time. | Kalyn Delaney (Primary Author) | Snider House | Thriller | 835 | 1 |
| Any element of a tuple can be accessed in constant time. | Wilfred Noble (Primary Author) | O'connor Books | Fiction, Graphic Novel, Horror | 989 | 1 |
| Any element of a tuple can be accessed in constant time. | Adelle Mejia (Primary Author), Jona Davidson (Co-Author) | Powell Publishing | Travel | 182 | 2 |
| Any element of a tuple can be accessed in constant time. | Freeda Kirby (Primary Author) | Simon Books | Philosophy | 729 | 1 |
| Any element of a tuple can be accessed in constant time. | Burton Robinson (Primary Author) | Robinson Books | Fantasy, Mystery | 135 | 1 |
Optimized
3 queries
22.5 ms
107 queries reduced to 3
(71.9ms to 22.5ms)
| Title | Authors | Publisher | Genres | Pages | # Authors |
|---|---|---|---|---|---|
| Messages can be sent to and received from ports, but these messages must obey the so-called "port protocol." | Marvella Franco (Primary Author) | Wilkerson Media | Poetry | 780 | 1 |
| He looked inquisitively at his keyboard and wrote another sentence. | Candra Barrera (Primary Author) | Houston Books | Cooking, Young Adult | 452 | 1 |
| Erlang is known for its designs that are well suited for systems. | Claretha Marsh (Primary Author), Clifford Wise (Co-Author) | Santiago Media | Cooking, Romance | 471 | 2 |
| Type classes first appeared in the Haskell programming language. | Napoleon Erickson (Primary Author) | Burton Publishing | Psychology | 976 | 1 |
| The sequential subset of Erlang supports eager evaluation, single assignment, and dynamic typing. | Leeanna Warren (Primary Author) | Randolph House | Mystery, Science Fiction | 541 | 1 |
| She spent her earliest years reading classic literature, and writing poetry. | Desmond Chavez (Primary Author) | Hunter Publishing | Fiction, Science Fiction | 1127 | 1 |
| The sequential subset of Erlang supports eager evaluation, single assignment, and dynamic typing. | Hyon Lindsey (Primary Author) | Brady Media | Science | 911 | 1 |
| Its main implementation is the Glasgow Haskell Compiler. | Napoleon Erickson (Primary Author) | Valentine House | Self-Help | 307 | 1 |
| Messages can be sent to and received from ports, but these messages must obey the so-called "port protocol." | Clifford Wise (Primary Author), Nyla Mcbride (Editor) | Bennett House | Non-Fiction | 149 | 2 |
| Haskell features a type system with type inference and lazy evaluation. | Lawrence Stout (Primary Author) | Bowers Books | Thriller | 223 | 1 |
| Do you have any idea why this is not working? | Chanell Carter (Primary Author) | Mathis Publishing | Fiction | 571 | 1 |
| The syntax {D1,D2,...,Dn} denotes a tuple whose arguments are D1, D2, ... Dn. | Houston Gill (Primary Author), Rema Cooley (Illustrator), Sergio Hamilton (Illustrator) | Wilkerson Media | History, Mystery | 391 | 3 |
| The arguments can be primitive data types or compound data types. | Kraig Fry (Primary Author) | Mcleod Books | Poetry, Self-Help | 912 | 1 |
| Messages can be sent to and received from ports, but these messages must obey the so-called "port protocol." | Delila Galloway (Primary Author), Charise Wilder (Co-Author) | Figueroa Media | Children's, Cooking, Fiction | 487 | 2 |
| The Galactic Empire is nearing completion of the Death Star, a space station with the power to destroy entire planets. | Shaunna Walters (Primary Author) | O'donnell Books | Children's, Science | 497 | 1 |
| Ports are created with the built-in function open_port. | Kristeen Gill (Primary Author), Charise Wilder (Illustrator) | Perry House | Philosophy | 430 | 2 |
| I don't even care. | Dwayne Short (Primary Author) | Knox Books | Biography, Poetry | 856 | 1 |
| The arguments can be primitive data types or compound data types. | Burton Robinson (Primary Author) | Nolan House | Science | 192 | 1 |
| They are written as strings of consecutive alphanumeric characters, the first character being lowercase. | Claretha Marsh (Primary Author) | Rollins Press | Fiction, Poetry | 510 | 1 |
| Erlang is known for its designs that are well suited for systems. | Candra Barrera (Primary Author) | Dudley Press | Graphic Novel, Horror | 398 | 1 |