-
Notifications
You must be signed in to change notification settings - Fork 161
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
LG-15119 query optimization #11574
LG-15119 query optimization #11574
Conversation
Adjust query and add index
partner_account_status: Agreements::PartnerAccountStatus.find_by(name: 'active'), | ||
became_partner: ..report_date, | ||
}). | ||
distinct |
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.
this has a 2x speed improvement
@facial_match_issuers ||= Profile.active.verified.facial_match. | ||
where('verified_at <= ?', report_date.end_of_day). | ||
distinct. | ||
pluck(:initiating_service_provider_issuer) |
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.
I think this is now 6x faster
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.
nice find!!
def change | ||
add_index :profiles, %i[active idv_level verified_at], algorithm: :concurrently, name: 'index_profiles_on_active_and_idv_level_and_verified_at' | ||
end | ||
end |
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.
This shouldn't have too many writes. It should significantly speed up the query above.
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.
Does the report run fast enough without the index?
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.
I think so. Are you concerned about the writes for this?
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.
I think in general we should avoid creating indexes if possible. The current query that would use this runs quickly enough, and we'll hopefully be able to avoid using Postgres for it soon.
Co-authored-by: Zach Margolis <[email protected]>
ef9d53c
to
49038c6
Compare
require "rubygems" | ||
require "bundler/setup" | ||
|
||
load Gem.bin_path("rspec-core", "rspec") |
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.
helpful to not have to type 'bundle exec'
Profile.where(active: true).where( | ||
'verified_at <= ?', | ||
end_date, | ||
Profile.active.where( |
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.
Updated most of this file to use the available class methods.
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.
Nit to update the PR description prior to merge since it says it adds an index but no longer does.
🤜🤛 for both speeding up queries and making the code easier to read.
pluck(:initiating_service_provider_issuer).uniq | ||
@facial_match_issuers ||= Reports::BaseReport.transaction_with_timeout do | ||
Profile.active.verified.facial_match_opt_in. | ||
where('verified_at <= ?', report_date.end_of_day). |
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 the verified
scope on the prior line redundant with this?
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.
yeah, it can't be active without verified so idk why I am the way that I am.
Adjust queries and add index
Link to the relevant ticket:
LG-15119
🛠 Summary of changes
Noticed a timeout in the PG query. We were pulling all profiles and using ruby .uniq instead of .distinct. This needed wrapped with the read_replica time_out method.
I've also added a new IDV Facial Match level to be IAL2 opt-in and updated the queries with it.