Commit 657b24b7 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve performance of stages index migration on PostgreSQL

parent d9f05306
...@@ -11,15 +11,34 @@ module Gitlab ...@@ -11,15 +11,34 @@ module Gitlab
end end
def perform(start_id, stop_id) def perform(start_id, stop_id)
sql = <<~SQL if Gitlab::Database.postgresql?
UPDATE ci_stages sql = <<~SQL
SET index = WITH freqs AS (
(SELECT stage_idx FROM ci_builds SELECT stage_id, stage_idx, COUNT(*) AS freq FROM ci_builds
WHERE ci_builds.stage_id = ci_stages.id WHERE stage_id BETWEEN #{start_id.to_i} AND #{stop_id.to_i}
GROUP BY ci_builds.stage_idx ORDER BY COUNT(*) DESC LIMIT 1) AND stage_idx IS NOT NULL
WHERE ci_stages.id BETWEEN #{start_id.to_i} AND #{stop_id.to_i} GROUP BY stage_id, stage_idx
AND ci_stages.index IS NULL ), indexes AS (
SQL SELECT DISTINCT stage_id, last_value(stage_idx)
OVER (PARTITION BY stage_id ORDER BY freq ASC) AS index
FROM freqs
)
UPDATE ci_stages SET index = indexes.index
FROM indexes WHERE indexes.stage_id = ci_stages.id
AND ci_stages.index IS NULL;
SQL
else
sql = <<~SQL
UPDATE ci_stages
SET index =
(SELECT stage_idx FROM ci_builds
WHERE ci_builds.stage_id = ci_stages.id
GROUP BY ci_builds.stage_idx ORDER BY COUNT(*) DESC LIMIT 1)
WHERE ci_stages.id BETWEEN #{start_id.to_i} AND #{stop_id.to_i}
AND ci_stages.index IS NULL
SQL
end
ActiveRecord::Base.connection.execute(sql) ActiveRecord::Base.connection.execute(sql)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment