Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FFT-95 Optimise edit forecast database queries #546

Merged
merged 8 commits into from
Nov 29, 2024
Merged

FFT-95 Optimise edit forecast database queries #546

merged 8 commits into from
Nov 29, 2024

Conversation

SamDudley
Copy link
Contributor

@SamDudley SamDudley commented Nov 15, 2024

I set out to improve the performance of the edit forecast page. On my local machine it took over 6 seconds just to fetch the initial HTML.

Our local dataset contains over 400 rows in the table on that page, so I suspected we had an n+1 database query problem. After some digging it turns out I was right and there were over 17,000 queries!

After a couple of days spent optimising code, mostly through the use of select_related and prefetch_related, I've managed to get the number of queries down to 13 and the page load time down to around 300ms!

Here is a brief list of the optimisations:

  • apply select_related and prefetch_related where needed
  • store the current_financial_year on the request object
  • apply cached_property to view methods being used in the template
  • pass the current_financial_year as context into the financial code serializer
  • add budget amount annotation to queryset
  • small javascript refactor to avoid needless object creation

There was a more ambitious optimising around the current_financial_year using a ContextVar, but I chose to leave it out and go with storing it on the request object as it's simpler.

The react code is now the bottleneck on that page :)

@SamDudley SamDudley requested a review from a team as a code owner November 15, 2024 18:24
@SamDudley SamDudley marked this pull request as draft November 15, 2024 18:24
@SamDudley SamDudley marked this pull request as ready for review November 29, 2024 12:16
so far so good. the issue is too many db queries.

started at 9000+ now down to 450~.

next steps is to look at the get_budget call
easy annotate sum for the budget has solved the n+1 finally

turned my attention to the rest of the view

added a selected_related

room for more improvements

down to 21 queries and no n+1
small js optimisation
add sentry database query tracking
@SamDudley SamDudley changed the title FFT-95 Optimise edit forecast requests FFT-95 Optimise edit forecast database queries Nov 29, 2024
@SamDudley SamDudley merged commit d477e98 into main Nov 29, 2024
4 checks passed
@SamDudley SamDudley deleted the fft-95 branch November 29, 2024 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants