Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimized Django Queryset

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!

like image 516
jape Avatar asked Nov 26 '25 13:11

jape


1 Answers

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

like image 196
Tommaso Barbugli Avatar answered Nov 30 '25 22:11

Tommaso Barbugli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!