summaryrefslogtreecommitdiff
path: root/db/migrate/20180119160751_optimize_ci_job_artifacts.rb
diff options
context:
space:
mode:
authorGreg Stark <stark@gitlab.com>2018-01-19 12:44:27 +0000
committerGreg Stark <stark@gitlab.com>2018-02-08 10:08:52 +0100
commit271e7a325340551475ae937aaf2ed7a6344be9e8 (patch)
tree62998df6dab77696d065cbb2c05a4a3370d5bd03 /db/migrate/20180119160751_optimize_ci_job_artifacts.rb
parentdbb934c8e2b5d138721f9d89afcf8ebbf814bed2 (diff)
downloadgitlab-ce-271e7a325340551475ae937aaf2ed7a6344be9e8.tar.gz
Add indexes and change SQL for expired artifacts to deal with artifacts migration efficiently
Artifacts are in the middle of being migrated from ci_builds to ci_job_artifacts. The expiration date is currently visible in both of these tables and the test for whether an expired artifact is present for a job is complex as it requires checking both the of the tables. Add two new indexes, one on ci_builds.artifacts_expire_at and one on ci_job_artifacts.expire_at to enable finding expired artifacts efficiently. And until the migration is finished, replace the SQL for finding expired and non-expired artifacts with a hand-crafted UNION ALL based query instead of using OR. This overcomes a database optimizer limitation that prevents it from using these indexes. When the migration is finished the next version should remove this query and replace it with a much simpler query on just ci_job_artifacts. See https://gitlab.com/gitlab-org/gitlab-ce/issues/42561 for followup.
Diffstat (limited to 'db/migrate/20180119160751_optimize_ci_job_artifacts.rb')
-rw-r--r--db/migrate/20180119160751_optimize_ci_job_artifacts.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/db/migrate/20180119160751_optimize_ci_job_artifacts.rb b/db/migrate/20180119160751_optimize_ci_job_artifacts.rb
new file mode 100644
index 00000000000..9b4340ed7b7
--- /dev/null
+++ b/db/migrate/20180119160751_optimize_ci_job_artifacts.rb
@@ -0,0 +1,23 @@
+# See http://doc.gitlab.com/ce/development/migration_style_guide.html
+# for more information on how to write migrations for GitLab.
+
+class OptimizeCiJobArtifacts < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ # Set this constant to true if this migration requires downtime.
+ DOWNTIME = false
+
+ disable_ddl_transaction!
+
+ def up
+ # job_id is just here to be a covering index for index only scans
+ # since we'll almost always be joining against ci_builds on job_id
+ add_concurrent_index(:ci_job_artifacts, [:expire_at, :job_id])
+ add_concurrent_index(:ci_builds, [:artifacts_expire_at], where: "artifacts_file <> ''")
+ end
+
+ def down
+ remove_concurrent_index(:ci_job_artifacts, [:expire_at, :job_id])
+ remove_concurrent_index(:ci_builds, [:artifacts_expire_at], where: "artifacts_file <> ''")
+ end
+end