Borrow Records

Loading 20 records. Compare query count and execution time between the two approaches.

Problem

The template accesses record.user.email and record.book.title — both ForeignKeys. With no optimization, each borrow record fires 2 separate queries to fetch its related user and book. Result: 2N+1 queries.

BorrowRecord.objects.all()

Fix

select_related('user', 'book') joins all three tables (borrow_records, users, books) in one SQL query. The is_overdue property reads fields already on the model instance — zero extra queries.

BorrowRecord.objects.select_related('user', 'book')

Unoptimized

41 queries 45.5 ms
N+1 detected: 41 queries for 20 rows
User Book Borrowed Due Overdue?
[email protected] Erlang is known for its designs that are well suited for systems. Jul 21, 2026 Feb 27, 2026 False
[email protected] Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. Jul 21, 2026 Aug 03, 2026 True
[email protected] Haskell features a type system with type inference and lazy evaluation. Jul 21, 2026 May 13, 2026 False
[email protected] I don't even care. Jul 21, 2026 Aug 02, 2026 True
[email protected] Ports are used to communicate with the external world. Jul 21, 2026 Apr 27, 2026 False
[email protected] Ports are used to communicate with the external world. Jul 21, 2026 Jul 11, 2026 False
[email protected] She spent her earliest years reading classic literature, and writing poetry. Jul 21, 2026 Jun 13, 2026 False
[email protected] Its main implementation is the Glasgow Haskell Compiler. Jul 21, 2026 Apr 25, 2026 False
[email protected] Atoms can contain any character if they are enclosed within single quotes and an escape convention exists which allows any character to be used within an atom. Jul 21, 2026 Jul 31, 2026 True
[email protected] He looked inquisitively at his keyboard and wrote another sentence. Jul 21, 2026 Mar 07, 2026 False
[email protected] Ports are used to communicate with the external world. Jul 21, 2026 Jul 29, 2026 True
[email protected] Erlang is a general-purpose, concurrent, functional programming language. Jul 21, 2026 Jul 31, 2026 True
[email protected] Atoms are used within a program to denote distinguished values. Jul 21, 2026 Aug 04, 2026 True
[email protected] The Galactic Empire is nearing completion of the Death Star, a space station with the power to destroy entire planets. Jul 21, 2026 Jul 30, 2026 True
[email protected] Do you come here often? Jul 21, 2026 Jul 31, 2026 False
[email protected] Do you have any idea why this is not working? Jul 21, 2026 Apr 18, 2026 False
[email protected] Atoms are used within a program to denote distinguished values. Jul 21, 2026 Mar 15, 2026 False
[email protected] She spent her earliest years reading classic literature, and writing poetry. Jul 21, 2026 Jul 29, 2026 True
[email protected] Erlang is known for its designs that are well suited for systems. Jul 21, 2026 Jun 14, 2026 False
[email protected] Its main implementation is the Glasgow Haskell Compiler. Jul 21, 2026 Mar 22, 2026 False

Optimized

1 queries 16.0 ms
41 queries reduced to 1 (45.5ms to 16.0ms)
User Book Borrowed Due Overdue?
[email protected] Erlang is known for its designs that are well suited for systems. Jul 21, 2026 Feb 27, 2026 False
[email protected] Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. Jul 21, 2026 Aug 03, 2026 True
[email protected] Haskell features a type system with type inference and lazy evaluation. Jul 21, 2026 May 13, 2026 False
[email protected] I don't even care. Jul 21, 2026 Aug 02, 2026 True
[email protected] Ports are used to communicate with the external world. Jul 21, 2026 Apr 27, 2026 False
[email protected] Ports are used to communicate with the external world. Jul 21, 2026 Jul 11, 2026 False
[email protected] She spent her earliest years reading classic literature, and writing poetry. Jul 21, 2026 Jun 13, 2026 False
[email protected] Its main implementation is the Glasgow Haskell Compiler. Jul 21, 2026 Apr 25, 2026 False
[email protected] Atoms can contain any character if they are enclosed within single quotes and an escape convention exists which allows any character to be used within an atom. Jul 21, 2026 Jul 31, 2026 True
[email protected] He looked inquisitively at his keyboard and wrote another sentence. Jul 21, 2026 Mar 07, 2026 False
[email protected] Ports are used to communicate with the external world. Jul 21, 2026 Jul 29, 2026 True
[email protected] Erlang is a general-purpose, concurrent, functional programming language. Jul 21, 2026 Jul 31, 2026 True
[email protected] Atoms are used within a program to denote distinguished values. Jul 21, 2026 Aug 04, 2026 True
[email protected] The Galactic Empire is nearing completion of the Death Star, a space station with the power to destroy entire planets. Jul 21, 2026 Jul 30, 2026 True
[email protected] Do you come here often? Jul 21, 2026 Jul 31, 2026 False
[email protected] Do you have any idea why this is not working? Jul 21, 2026 Apr 18, 2026 False
[email protected] Atoms are used within a program to denote distinguished values. Jul 21, 2026 Mar 15, 2026 False
[email protected] She spent her earliest years reading classic literature, and writing poetry. Jul 21, 2026 Jul 29, 2026 True
[email protected] Erlang is known for its designs that are well suited for systems. Jul 21, 2026 Jun 14, 2026 False
[email protected] Its main implementation is the Glasgow Haskell Compiler. Jul 21, 2026 Mar 22, 2026 False

Back to all queries