melizalab / django-bird-colony

Bird colony management tool
BSD 3-Clause "New" or "Revised" License
4 stars 1 forks source link

Make "alive" annotation optional for Animal model to speed up counts of children. #31

Closed dmeliza closed 5 months ago

dmeliza commented 5 months ago

The default manager for this class annotates queries with an 'alive' field that's calculated by summing "adds" vs "removes" over the events associated with the animal. This field is used in a lot of views, but it's not needed to for example count the number of children (including the number of living and unhatched, as that can be done with a simple exclude or filter on the status.

I think this annotation is costing a lot for some views because a children query gets called for every animal in a list. It affects not just the top-level lists of animals but also the list of children shown for views of individual animals.

I'm actually a little surprised that the annotation comes through on a related object query. But the SQL queries suggest that the children queries are getting hit a lot.

I'm not sure the best way to solve this, because the "alive" annotation is needed whenever we do a list of birds. It just doesn't need to be known when we look up the number of children. Maybe a separate method to do the child counts on unannotated queries. Alternatively, calculate these numbers in the database. Essentially every animal view requires joins against the event table, both directly and via its children and/or parents.