Skip to content
Tuto Django logo
Tuto Django (2022-08-04 07H (Europe/Paris)) Understanding select_related and prefetch_related in Django by akshaythekkath (https://hashnode.com/@akshaythekkath)
Type to start searching
Tuto Django
  • Index
  • Awsome Django
  • Tuto HTMX
  • Tuto HTTP
  • Tuto devops
  • Django performances
  • Performance articles
  • Performance articles 2021
Tuto Django
  • Django news
  • News
  • Django architecture
  • DEPs
  • Installation
  • Doc
  • Django people
  • Community
  • Django with htmx
  • Django performances
    • related + prefetch class
    • Django performance tools
    • Performance articles
      • Performance articles 2021
        • Understanding select_related and prefetch_related in Django by akshaythekkath (https://hashnode.com/@akshaythekkath)
          • Understanding select_related and prefetch_related in Django by akshaythekkath (https://hashnode.com/@akshaythekkath)
            • Introduction
          • Show Source
          • Introduction
        • Go Fast With Django
      • Performance articles 2017
  • Tests
  • Django tips
  • Tools
  • Django versions
  • Versions
  • Meta informations
  • Meta doc
  • Understanding select_related and prefetch_related in Django by akshaythekkath (https://hashnode.com/@akshaythekkath)
    • Introduction
  • Show Source

Understanding select_related and prefetch_related in Django by akshaythekkath ( https://hashnode.com/@akshaythekkath ) ¶

  • https://blog.akshaythekkath.com/understanding-selectrelated-and-prefetchrelated-in-django

Introduction ¶

When I started learning Django, the one thing that fascinated me a lot was the Django ORM.

Maybe I was happy that I will not have to write SQL queries and can skip all the pain of writing crazy joins to fetch the data.

I was throwing in objects.all() and objects.filter(…) with joy all around my application. Until I reached a point where I had a lot of data in the system and it started to lag .

Sometimes as ORM users, we don’t bother to see the underlying SQL query that is being built, or how many queries are fired ? which becomes expensive over time.

My perspective started to change . I started thinking in SQL first and then how to query the Django ORM so that it builds the desired SQL query. With this approach, I started optimizing the queries and the latency of my APIs began to drop.

Previous Performance articles 2021
Next Go Fast With Django
© Copyright 2018-2022, DevOps people Built with sphinx 5.1.1 Python 3.10.4.
Last updated on Aug 04, 2022.
Created using Sphinx 5.1.1. and Material for Sphinx