[Answer]-Django select related performance

1👍

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).

Leave a comment