-
-
Notifications
You must be signed in to change notification settings - Fork 503
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
Perf: refactor DistributionsByCountry#report using raw SQL query #4841
base: main
Are you sure you want to change the base?
Perf: refactor DistributionsByCountry#report using raw SQL query #4841
Conversation
i should review the functionality once any changes re approach are made. (No point until then) |
226d9c9
to
245a805
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the approach overall!
@@ -0,0 +1,78 @@ | |||
class DistributionSummaryByCountyQuery |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason this needs to be an instance instead of just having a class function? It's easier to test, stub and work with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also can we return a data class instead of a hash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason this needs to be an instance instead of just having a class function? It's easier to test, stub and work with.
No real reason, current queries in this folder vary (sometimes call
is an instance method sometimes a class method).
Changed it to be a class method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also can we return a data class instead of a hash?
Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks good to me, although I'm a bit concerned at the test changes. Performance-only fixes shouldn't need to change tests - by definition the way you know it didn't break anything is when the exact same tests pass as before. Can you explain why they were changed?
I didn't touch the related system or request specs for this report/page. But since I moved the core logic from a service to a query object, so I figured some refactoring of the test was ok. I left the actual assertions the same, but I refactored the setup. The old setup was including setup from elsewhere ( |
@coalest I'd really appreciate it if for this PR you could leave the tests as is so we can validate the success of the goal of this PR, which is improving performance without changing anything else. |
Doesn't resolve any issue.
Description
One of the pages that was reported to be slow in production is the
distributions_by_country#report
page.I tried out two approaches:
Approach number 2 seems faster, but potentially less maintainable. Approach number 1 still helps dramatically though.
Request times mentioned below are just for comparison purposes (I still just have the seed data locally).
Type of change
How Has This Been Tested?
Screenshots
Before any changes (~280 ms):
After fixing the N+1's using by adding associations to
includes
(~120 ms):View diff of what fixing N+1's look like
After rewriting with raw SQL (~60 ms):