From d3a3db4218e10c65c2e87c763ad02169f3736883 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Thu, 1 Aug 2019 17:18:17 +0100 Subject: Speed up loading and filtering deploy keys and their projects This commit changes how we eager-load projects, routes, and namespaces required by the deploy keys endpoint, getting a 10x improvement in my local testing. The endpoint still doesn't scale in-general, but going from ~13 seconds to dump a 63K result to generating the same thing in ~1.6 seconds seems like a good improvement to me. --- app/models/deploy_key.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'app/models/deploy_key.rb') diff --git a/app/models/deploy_key.rb b/app/models/deploy_key.rb index fb50da39ca3..0bd90bd28e3 100644 --- a/app/models/deploy_key.rb +++ b/app/models/deploy_key.rb @@ -9,6 +9,7 @@ class DeployKey < Key scope :in_projects, ->(projects) { joins(:deploy_keys_projects).where('deploy_keys_projects.project_id in (?)', projects) } scope :are_public, -> { where(public: true) } + scope :with_projects, -> { includes(deploy_keys_projects: { project: [:route, :namespace] }) } ignore_column :can_push @@ -23,7 +24,7 @@ class DeployKey < Key end def almost_orphaned? - self.deploy_keys_projects.length == 1 + self.deploy_keys_projects.count == 1 end def destroyed_when_orphaned? @@ -47,6 +48,6 @@ class DeployKey < Key end def projects_with_write_access - Project.preload(:route).where(id: deploy_keys_projects.with_write_access.select(:project_id)) + Project.with_route.where(id: deploy_keys_projects.with_write_access.select(:project_id)) end end -- cgit v1.2.3