<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Satish Baradiya on Medium]]></title>
        <description><![CDATA[Stories by Satish Baradiya on Medium]]></description>
        <link>https://medium.com/@baradiyasatish?source=rss-2e71af877452------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*nGNwfEUSwfm3vFKn</url>
            <title>Stories by Satish Baradiya on Medium</title>
            <link>https://medium.com/@baradiyasatish?source=rss-2e71af877452------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:58:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@baradiyasatish/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Optimizing Django Queries with select_related and prefetch_related.]]></title>
            <link>https://medium.com/@baradiyasatish/optimizing-django-queries-with-select-related-and-prefetch-related-22ee02015f72?source=rss-2e71af877452------2</link>
            <guid isPermaLink="false">https://medium.com/p/22ee02015f72</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[optmization]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Satish Baradiya]]></dc:creator>
            <pubDate>Sat, 28 Oct 2023 12:31:36 GMT</pubDate>
            <atom:updated>2023-10-28T12:31:36.479Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the key feature of django is Object Relational Mapping(ORM) system, which simplifies database operations. However, as application grow, the performance of the database queries can become a concern. To optimize your queries and reduce database hits, django provides two essential tools: select_related and prefetch_related. In this post we will explore how this two methods can be used to improve the efficiency of django queries.</p><p><strong>Understanding the problem:</strong></p><p>In Django, database queries are typically executed lazily. This means that when you access a related object or field, a new database query is triggered. If you have a list of items with multiple related objects, such as a <strong>ForeignKey</strong> or <strong>ManyToManyField</strong>, accessing those related objects for each item in the list can result in large number of database queries. This can lead to N+1 query problem, where N is the number of items in your list.</p><p><strong>“select_related” — Reducing the N+1 Query problem.</strong></p><p>select_related is used to perform SQL join and retrieve related objects in a single query. It’s most effective when you have a <strong>“ForeignKey”</strong> or a <strong>“OneToOneField”</strong> in your model.</p><p><strong>Example:</strong></p><p>Let’s say you have two models: <strong>“Author”</strong> and <strong>“Book”</strong>, with a foreign key relationship. You want to retrieve all books along with their authors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*G1Wxi4AgGsg3PVFe4Suzgw.png" /><figcaption>Using select_related to optimize query.</figcaption></figure><p>In the first example, without select_related, a separate query is made for each book’s author, resulting in N+1 problem. In the second example using select_related , a single query joins the “Book” and “Author” tables, eliminating the need for additional queries.</p><p><strong>“prefetch_related” — Optimizing Many-to-Many and Reverse Foreign Key Relationships.</strong></p><p>prefetch_related is used when optimizing queries with many-to-many and reverse foreign key relationships. It performs two separate queries, one for the main object and one for the related objects. Then, it associates them in Python, avoiding the N+1 query problem.</p><p><strong>Example:</strong></p><p>Suppose you have models for <strong>“Category”</strong> and <strong>“Product”</strong>, connected by many-to-many relationship. You want to retrieve all categories and their associated products.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*v1k2imgIcNAigA87e40aLg.png" /><figcaption>Using prefetch_related to optimize query.</figcaption></figure><p>In the first example, without prefetch_related, a separate query is made for each category’s product, leading to N+1 problem. In the second example, using and prefetch_related, two queries are executed: one for categories and one for products. Then Django associates the products with their respective categories in Python, eliminating need for extra queries.</p><h3>Conclusion</h3><p>In summary, Django’s select_related and prefetch_related are powerful tools for optimizing your application&#39;s database queries. When used correctly, they can significantly reduce the number of database hits, improving the performance and responsiveness of your Django application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22ee02015f72" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>