DDMAL / CantusDB

A new site for Cantus Database running under Django.
https://cantusdatabase.org
MIT License
5 stars 6 forks source link

We should do a census of our views to ensure querysets are optimized #1253

Open jacobdgm opened 9 months ago

jacobdgm commented 9 months ago

@ahankinson recently opened #1247 and #1248 (thank you!), adding .select_related to some key querysets and thus significantly reducing the number of database queries required to serve these pages.

About a year ago, we identified a bunch of views that were loading slowly and optimized them using .select_related, .prefetch_related, .values, and so on. It would be worth systematically checking all our views to ensure these optimizations are being consistently applied.

ahankinson commented 9 months ago

I don’t think it’s that complicated. I was able to do those fixes in a few minutes. My patented technique consists of:

The thing that will require more in-depth rewrites, though, are the queries within for loops. I see a common pattern of:

Those queries can’t be optimized easily, but they could probably be refactored to get all the records in one go. If necessary the records could be fetched and then a non-query loop can sort them into nested lists or whatever is necessary.

The other thing I see is naive use of query set methods within templates. It’s not uncommon to come across things like source.chants.all, which will trigger a database query if not properly handled in the view first.