Django Tips : ๐—ข๐—ฝ๐˜๐—ถ๐—บ๐—ถ๐˜‡๐—ถ๐—ป๐—ด ๐—ฌ๐—ผ๐˜‚๐—ฟ ๐—ค๐˜‚๐—ฒ๐—ฟ๐˜† ๐—ฃ๐—ฒ๐—ฟ๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐—ป๐—ฐ๐—ฒ

mahabub.devs3
Mahabubur Rahman
Published on Nov, 17 2024 2 min read 0 comments
image

Did you know that understanding Django's select_related and prefetch_related can make or break your query performance? 

Hereโ€™s a guide on ๐˜€๐—ฒ๐—น๐—ฒ๐—ฐ๐˜_๐—ฟ๐—ฒ๐—น๐—ฎ๐˜๐—ฒ๐—ฑ and ๐—ฝ๐—ฟ๐—ฒ๐—ณ๐—ฒ๐˜๐—ฐ๐—ต_๐—ฟ๐—ฒ๐—น๐—ฎ๐˜๐—ฒ๐—ฑ:

๐˜€๐—ฒ๐—น๐—ฒ๐—ฐ๐˜_๐—ฟ๐—ฒ๐—น๐—ฎ๐˜๐—ฒ๐—ฑ : The select_related method in Django is used to create SQL joins and retrieve related objects in a single query, which can significantly improve performance by reducing the number of database hits.

Hereโ€™s an example to illustrate how select_related works:

Letโ€™s say you have the following models:

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author, related_name='books', on_delete=models.CASCADE)

If you want to fetch all books along with their authors, you can use select_related like this:


# Without select_related
books = Book.objects.all()
for book in books:
    author_name = book.author.name  # This will hit the database for each book

# With select_related
books = Book.objects.select_related('author').all()
for book in books:
    author_name = book.author.name  # This will not hit the database again

In the first example, each time you access book.author.name, a new query is made to the database. In the second example, select_related fetches all related authors in a single query, significantly reducing the number of database hits.

 

๐—ฝ๐—ฟ๐—ฒ๐—ณ๐—ฒ๐˜๐—ฐ๐—ต_๐—ฟ๐—ฒ๐—น๐—ฎ๐˜๐—ฒ๐—ฑ : The prefetch_related method in Django is used to reduce the number of database queries by performing a single query for related objects. This is particularly useful when you have a ForeignKey or ManyToManyField and you want to fetch related objects efficiently.

Hereโ€™s a simple example to illustrate how prefetch_related works:

Letโ€™s say you have the following models:

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author, related_name='books', on_delete=models.CASCADE)

If you want to fetch all authors and their related books, you can use prefetch_related like this:

# Without prefetch_related
authors = Author.objects.all()
for author in authors:
    books = author.books.all()  # This will hit the database for each author

# With prefetch_related
authors = Author.objects.prefetch_related('books')
for author in authors:
    books = author.books.all()  # This will not hit the database again

In the first example, each time you access author.books.all(), a new query is made to the database. In the second example, prefetch_related fetches all related books in a single query, significantly reducing the number of database hits.

0 Comments