What's the difference between select_related and prefetch_related in Django ORM?

NeoWang

In Django doc,

select_related() "follows" foreign-key relationships, selecting additional related-object data when it executes its query.

prefetch_related() does a separate lookup for each relationship, and does the "joining" in Python.

What does it mean by "doing the joining in python"? Can someone illustrate with an example?

My understanding is that for foreign key relationship, use select_related; and for M2M relationship, use prefetch_related. Is this correct?

CrazyCasta

Your understanding is mostly correct. You use select_related when the object that you're going to be selecting is a single object, so OneToOneField or a ForeignKey. You use prefetch_related when you're going to get a "set" of things, so ManyToManyFields as you stated or reverse ForeignKeys. Just to clarify what I mean by "reverse ForeignKeys" here's an example:

class ModelA(models.Model):
    pass

class ModelB(models.Model):
    a = ForeignKey(ModelA)

ModelB.objects.select_related('a').all() # Forward ForeignKey relationship
ModelA.objects.prefetch_related('modelb_set').all() # Reverse ForeignKey relationship

The difference is that select_related does an SQL join and therefore gets the results back as part of the table from the SQL server. prefetch_related on the other hand executes another query and therefore reduces the redundant columns in the original object (ModelA in the above example). You may use prefetch_related for anything that you can use select_related for.

The tradeoffs are that prefetch_related has to create and send a list of IDs to select back to the server, this can take a while. I'm not sure if there's a nice way of doing this in a transaction, but my understanding is that Django always just sends a list and says SELECT ... WHERE pk IN (...,...,...) basically. In this case if the prefetched data is sparse (let's say U.S. State objects linked to people's addresses) this can be very good, however if it's closer to one-to-one, this can waste a lot of communications. If in doubt, try both and see which performs better.

Everything discussed above is basically about the communications with the database. On the Python side however prefetch_related has the extra benefit that a single object is used to represent each object in the database. With select_related duplicate objects will be created in Python for each "parent" object. Since objects in Python have a decent bit of memory overhead this can also be a consideration.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django prefetch_related and select_related

From Dev

Django proper use of select_related or prefetch_related on a ForeignKey

From Dev

How to use prefetch_related with Django's DetailView

From Dev

How to use prefetch_related with Django's DetailView

From Dev

How to clear Django's cached query after prefetch_related

From Dev

Django: check if value in values_list with & without prefetch_related/select_related

From Dev

django select_related vs prefetch_related in tree like table

From Dev

select_related after prefetch_related

From Dev

Joining ManyToMany fields with prefetch_related in Django

From Dev

django prefetch_related id only

From Dev

Filter prefetch_related empty in django

From Dev

Is django prefetch_related supposed to work with GenericRelation

From Dev

django prefetch_related id only

From Dev

Django, general version of prefetch_related()?

From Dev

What's difference between ORM and ODBC?

From Dev

filter with select_related on Django

From Dev

django select_related in template

From Dev

Django select_related filter

From Dev

filter with select_related on Django

From Dev

django- Use prefetch_related inside of another prefetch_related

From Dev

prefetch_related with Django 1.5 + django-model-utils

From Java

django rest framework - backward serialization to avoid prefetch_related

From Dev

Django prefetch_related - filter with or-clause from different tables

From Dev

Django prefetch_related GenericForeignKey with multiple content types

From Dev

Django prefetch_related From Model With Multiple ManyToMany Relationships

From Dev

Django prefetch_related with m2m through relationship

From Dev

Django prefetch_related from foreignkey with manytomanyfield not working

From Dev

Django avoid extra queries using prefetch_related not working

From Dev

Django 1.9.1 prefetch_related with only hit multiple times to the database

Related Related

  1. 1

    Django prefetch_related and select_related

  2. 2

    Django proper use of select_related or prefetch_related on a ForeignKey

  3. 3

    How to use prefetch_related with Django's DetailView

  4. 4

    How to use prefetch_related with Django's DetailView

  5. 5

    How to clear Django's cached query after prefetch_related

  6. 6

    Django: check if value in values_list with & without prefetch_related/select_related

  7. 7

    django select_related vs prefetch_related in tree like table

  8. 8

    select_related after prefetch_related

  9. 9

    Joining ManyToMany fields with prefetch_related in Django

  10. 10

    django prefetch_related id only

  11. 11

    Filter prefetch_related empty in django

  12. 12

    Is django prefetch_related supposed to work with GenericRelation

  13. 13

    django prefetch_related id only

  14. 14

    Django, general version of prefetch_related()?

  15. 15

    What's difference between ORM and ODBC?

  16. 16

    filter with select_related on Django

  17. 17

    django select_related in template

  18. 18

    Django select_related filter

  19. 19

    filter with select_related on Django

  20. 20

    django- Use prefetch_related inside of another prefetch_related

  21. 21

    prefetch_related with Django 1.5 + django-model-utils

  22. 22

    django rest framework - backward serialization to avoid prefetch_related

  23. 23

    Django prefetch_related - filter with or-clause from different tables

  24. 24

    Django prefetch_related GenericForeignKey with multiple content types

  25. 25

    Django prefetch_related From Model With Multiple ManyToMany Relationships

  26. 26

    Django prefetch_related with m2m through relationship

  27. 27

    Django prefetch_related from foreignkey with manytomanyfield not working

  28. 28

    Django avoid extra queries using prefetch_related not working

  29. 29

    Django 1.9.1 prefetch_related with only hit multiple times to the database

HotTag

Archive