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

CAPT 983/slow loading payroll #3250

Merged
merged 10 commits into from
Nov 8, 2024
Merged

CAPT 983/slow loading payroll #3250

merged 10 commits into from
Nov 8, 2024

Conversation

rjlynch
Copy link
Contributor

@rjlynch rjlynch commented Oct 1, 2024

Speed up payroll run queries.

In order to shift more of the payroll query work to the database we need to be able to join to the various eligibility tables. To make this trickier the column name for award_amount is different on some policy's eligibilities (ie student_loans). This PR introduces a new claim scope with_award_amount that handles joining claims to the eligibilities tables and picking the correct column for the award amount.

Of note this PR makes the following assumptions (which are also made on master but it's worth calling them out)

  • A claim has one topup
  • A topup is always paid in a different payroll run to the claim

Benchmarks

Hitting the payroll index page
Master
:view_runtime=>6573.04, :db_runtime=>2113.22, :allocations=>13379102

This branch
:view_runtime=>248.16, :db_runtime=>517.15, :allocations=>481363

Payroll show for Nov 23, with 3k claims
Master
:view_runtime=>4479.93, :db_runtime=>325.29, :allocations=>10520859

This branch
:view_runtime=>189.02, :db_runtime=>187.99, :allocations=>311803

@rjlynch rjlynch force-pushed the CAPT-983/slow-loading-payroll branch 6 times, most recently from 7253886 to 479469c Compare October 4, 2024 13:39
app/models/payroll_run.rb Outdated Show resolved Hide resolved
@rjlynch rjlynch force-pushed the CAPT-983/slow-loading-payroll branch 2 times, most recently from 257cbc7 to 86ac927 Compare October 14, 2024 11:09
@rjlynch rjlynch marked this pull request as ready for review October 14, 2024 13:48
@rjlynch rjlynch force-pushed the CAPT-983/slow-loading-payroll branch from 86ac927 to 7958a55 Compare November 4, 2024 14:40
@@ -33,7 +33,7 @@ def create

# NOTE: Optimisation - preload payments, claims and eligibility
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# NOTE: Optimisation - preload payments, claims and eligibility

@@ -58,4 +58,12 @@ def further_education_payments?
def auto_check_student_loan_plan_task?
false
end

def award_amount_column
if to_s == "StudentLoans"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the StudentLoans model should override this method instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's nicer, have fixed 👍

Adds some specs to cover topup scenarios
Rather than looping through all payments each time we can just loop
through the payments for the provided policy.
Makes understanding what's going on easier
@Items doesn't need to be an instance variable
Doesn't speed things up as we're hitting the AR cache but cleans up the
noise in the logs
We hit this line items method multiple times with the same policy, cache
the results.
On my test data set this takes the page load from 7s to ~500ms
Refactors the CTE to build a generic eligibilities table we can join to.
Now when we add a new policy the payroll query should automatically pick
it up. Ideally we'd rename the "student_loan_repayment_amount" column in
the DB but that's quite involved, so for the time being we define an
'award_amount_column' column on the policy which returns the name of the
DB column we need to check to get the award amount.
Adds a new scope to claim  `with_award_amount` allowing for querying
claims in the db by their award amount.
@rjlynch rjlynch force-pushed the CAPT-983/slow-loading-payroll branch from 7958a55 to 24dcf92 Compare November 8, 2024 10:56
@rjlynch rjlynch merged commit 718079f into master Nov 8, 2024
14 checks passed
@rjlynch rjlynch deleted the CAPT-983/slow-loading-payroll branch November 8, 2024 11:00
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