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

Kill n+1 queries #41

Merged
merged 3 commits into from
Feb 27, 2017
Merged

Kill n+1 queries #41

merged 3 commits into from
Feb 27, 2017

Conversation

eisuke
Copy link
Member

@eisuke eisuke commented Feb 27, 2017

@cookpad/dev-infra please review

@eagletmt
Copy link
Member

any_token? defined at L29 seems unused now.

@eisuke
Copy link
Member Author

eisuke commented Feb 27, 2017

any_token? defined at L29 seems unused now.

✂️ 7e233dd

@@ -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?
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

looks nice 👍 4662535

Copy link

@aamine aamine left a comment

Choose a reason for hiding this comment

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

LGTM

@eisuke eisuke merged commit 7547f1c into cookpad:master Feb 27, 2017
@eisuke eisuke deleted the kill_n_plus_one branch February 27, 2017 08:22
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.

3 participants