0
0
Djangoframework~20 mins

Database query optimization with select_related in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Select Related Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding select_related effect on query count
Given the Django models Author and Book where Book has a foreign key to Author, what is the difference in database queries between these two code snippets?

1) books = Book.objects.all()
2) books = Book.objects.select_related('author').all()

What will be the number of queries executed when iterating over books and accessing book.author.name in each case?
Django
for book in books:
    print(book.author.name)
A1) Executes N+1 queries (N = number of books); 2) Executes 1 query
B1) Executes 1 query; 2) Executes N queries (N = number of books)
C1) Executes 1 query; 2) Executes 1 query
D1) Executes N queries; 2) Executes N+1 queries
Attempts:
2 left
💡 Hint
Think about how select_related joins tables to reduce queries.
📝 Syntax
intermediate
2:00remaining
Correct usage of select_related with multiple fields
Which of the following is the correct way to use select_related to fetch related fields author and publisher in a Django query?
ABook.objects.select_related(['author', 'publisher']).all()
BBook.objects.select_related('author', 'publisher').all()
CBook.objects.select_related('author__publisher').all()
DBook.objects.select_related('author').select_related('publisher').all()
Attempts:
2 left
💡 Hint
Check the expected argument type for select_related.
🔧 Debug
advanced
2:00remaining
Diagnosing unexpected query count with select_related
A developer uses select_related('author') on a queryset but still sees multiple queries when accessing book.author.profile. Why does this happen?
Aselect_related does not work with foreign keys, only with many-to-many fields
Bselect_related only fetches one level of foreign keys; author.profile is a second level relation not included
CThe developer must call select_related twice to fetch nested relations
Dselect_related causes Django to cache queries, so multiple queries are expected
Attempts:
2 left
💡 Hint
Think about how deep select_related fetches related objects.
🧠 Conceptual
advanced
2:00remaining
When to prefer select_related over prefetch_related
Which situation is best suited for using select_related instead of prefetch_related in Django?
AFetching many-to-many related objects where the related set is large
BFetching reverse foreign key relations with many related objects
CFetching unrelated models in separate queries
DFetching foreign key related objects where the relation is one-to-one or many-to-one
Attempts:
2 left
💡 Hint
Consider the type of relationship and how joins work.
state_output
expert
2:00remaining
Output of nested select_related query
Given these Django models:

class Country(models.Model): name = models.CharField(max_length=100) class Author(models.Model): name = models.CharField(max_length=100) country = models.ForeignKey(Country, on_delete=models.CASCADE) class Book(models.Model): title = models.CharField(max_length=100) author = models.ForeignKey(Author, on_delete=models.CASCADE)

What is the output of the following code snippet?

books = Book.objects.select_related('author__country').all() for book in books: print(f"{book.title} by {book.author.name} from {book.author.country.name}")

Assuming the database has 3 books with authors and countries properly set, how many database queries will this code execute?
Django
books = Book.objects.select_related('author__country').all()
for book in books:
    print(f"{book.title} by {book.author.name} from {book.author.country.name}")
A3 queries
B2 queries
C1 query
DN+1 queries (N = number of books)
Attempts:
2 left
💡 Hint
select_related can follow nested foreign keys with double underscores.