From 0f1452c2d1a59f8460b01eee7ae4a1ef51f41514 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Sat, 8 Apr 2017 23:56:13 +0200 Subject: Fix performance regression when displaying builds and pipelines --- app/models/commit_status.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb index 2c4033146bf..3e736641380 100644 --- a/app/models/commit_status.rb +++ b/app/models/commit_status.rb @@ -20,9 +20,14 @@ class CommitStatus < ActiveRecord::Base alias_attribute :author, :user scope :latest, -> do - max_id = unscope(:select).select("max(#{quoted_table_name}.id)") - - where(id: max_id.group(:name, :commit_id)) + latest_for_pipeline = <<-SQL.strip_heredoc + SELECT max(other_builds.id) + FROM #{quoted_table_name} other_builds + WHERE other_builds.commit_id = ci_builds.commit_id + GROUP BY other_builds.name + SQL + + where("#{quoted_table_name}.id IN (#{latest_for_pipeline})") end scope :failed_but_allowed, -> do -- cgit v1.2.1