Skip to content
Advertisement

Django View, foreign key field is killing performance

MyObjects definition:

JavaScript

View definition:

JavaScript

I do some calculations in my view that are irrelevant and my view takes around 3 seconds for 5000 objects if my_dict[str(my_obj.foreign_key_field)] += 1 is commented out. However, when that line uncommented, my view takes 20seconds. It must be because this field is a foreign key field as none of my other fields are foreign keys. That specific line is the only line referencing that field. How am I able to improve performance on this? I am surprised that just adding this line decreases my performance by that much, as objects is only around 5k and the foreign key table is around 50 objects.

No other operations are touching this dictionary because that specific line, so it’s not a cascading affect. If I comment out all logic besides that specific line

JavaScript

performance is still awful. Any ideas on how to improve?

Advertisement

Answer

you could optimize the query by one of the following steps:

1. Use select_related to reduce the number of database queries

JavaScript

2. Use prefetch_related to reduce the number of database queries

JavaScript

3. Use annotate to count objects in a single query

JavaScript

then

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement