-
Notifications
You must be signed in to change notification settings - Fork 70
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
Kill n+1 queries #41
Kill n+1 queries #41
Conversation
|
✂️ 7e233dd |
app/models/kuroko2/job_definition.rb
Outdated
@@ -107,7 +101,7 @@ def create_instance(script: nil, launched_by:, token: nil ) | |||
private | |||
|
|||
def confirm_active_instances | |||
if job_instances.any_token? | |||
if Kuroko2::Token.where(job_instance_id: job_instances.pluck(:id)).exists? |
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.
job_instances.pluck(:id)
could be very large. How about joining job_instances table?
irb(main):019:0> job_definition = Kuroko2::JobDefinition.last
Kuroko2::JobDefinition Load (2.2ms) SELECT "kuroko2_job_definitions".* FROM "kuroko2_job_definitions" ORDER BY "kuroko2_job_definitions"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Kuroko2::JobDefinition id: 6, ...>
irb(main):020:0> Kuroko2::Token.joins(:job_instance).merge(job_definition.job_instances).exists?
Kuroko2::Token Exists (2.9ms) SELECT 1 AS one FROM "kuroko2_tokens" INNER JOIN "kuroko2_job_instances" ON "kuroko2_job_instances"."id" = "kuroko2_tokens"."job_instance_id" WHERE "kuroko2_job_instances"."job_definition_id" = $1 LIMIT $2 [["job_definition_id", 6], ["LIMIT", 1]]
=> false
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.
looks nice 👍 4662535
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.
LGTM
@cookpad/dev-infra please review