3

I have the following function to determine who downloaded a certain book:

@cached_property
def get_downloader_info(self):
    return self.downloaders.select_related('user').values(
        'user__username', 'user__full_name')

Since I'm only using two fields, does it make sense to use .defer() on the remaining fields?

I tried to use .only(), but I get an error that some fields are not JSON serializable.

I'm open to all suggestions, if any, for optimizing this queryset.

Thank you!

jape
  • 2,861
  • 2
  • 26
  • 58

1 Answers1

2

Before you try every possible optimization, you should get your hands on the SQL query generated by the ORM (you can print it to stdout or use something like django debug toolbar) and see what is slow about it. After that I suggest you run that query with EXPLAIN ANALYZE and find out what is slow about that query. If the query is slow because lot of data has to be transfer than it makes lot of sense to use only or defer. Using only and defer (or values) gives you better performances only if you need to retrieve lot of data, but it does not make your database job much easier (unless you really have to read a lot of data of course).

Since you are using Django and Postgresql, you can get a psql session with manage.py dbshell and get query timings with \timing

Community
  • 1
  • 1
Tommaso Barbugli
  • 11,781
  • 2
  • 42
  • 41
  • Even if it only helped a little bit, wouldn't it make sense to optimize every aspect possible? I just installed the Django Debug Toolbar though, so thank you. – jape Dec 28 '15 at 19:00