Ryan Saxe
Ryan Saxe

Reputation: 17829

Django select related performance

So I am debating whether or not to use Django's select_related or not for performance issues.

In the documentation, it says that this is a "performance booster" because it does not need to query the database anymore, but that would clearly mean it has to store a lot more data locally, which can be exhaustive if you need to do a lot of separate calls for a lot of different users.

What are the pros and cons of performance with Django's select_related? And when should (or shouldn't) it be used?

Upvotes: 0

Views: 1050

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77902

If you dont use select_related or not you will eat memory each time you access a related object, so if you have to access related objects it won't make that much of a difference wrt/ memory usage and can indeed save a lot of db access cost - specially if your db server is not on the same node as your django instance(s). To make a long story short:

  • as a general guideline: use select_related (with appropriate params to limit what relationships should be followed) when you know you'll need the related object.
  • if in doubt, don't try to guess, test and profile (yes it requires quite some infrastructure to do proper testing and profiling here but hey, that's how it is).

My own experience: careful use of select_related can vastly improve execution time, never had a problem with memory but we usually do our best to avoid loading millions of rows when we just need a couple ones (doing proper filtering, slicing etc before the query is actually eval'd).

Upvotes: 1

Related Questions