summaryrefslogtreecommitdiff
path: root/db/migrate/20180213131630_add_partial_index_to_projects_for_index_only_scans.rb
diff options
context:
space:
mode:
authorAndreas Brandl <abrandl@gitlab.com>2018-02-13 14:24:24 +0100
committerAndreas Brandl <abrandl@gitlab.com>2018-02-20 13:08:24 +0100
commit5a5a33a9a6062bac2bded77c434cc56a4e68dac1 (patch)
tree9c5f36eefd62f588755bccb73b6a57718fc59012 /db/migrate/20180213131630_add_partial_index_to_projects_for_index_only_scans.rb
parent39d14f2f4bd30254e5777e05ad9a0f190f6f9182 (diff)
downloadgitlab-ce-5a5a33a9a6062bac2bded77c434cc56a4e68dac1.tar.gz
Add partial index on projects for index-only scans.
This helps with queries that get project ids based on the - comparably rare - visibility levels 10 and 20. For these, postgres can now leverage the partial index for a index-only scan to improve performance. Example queries: SELECT id FROM projects WHERE visibility_level IN (10,20) SELECT id FROM projects WHERE visibility_level IN (10) For MySQL, this results in a full index on id because MySQL omits the WHERE clause. That is, the index is a duplicate of the primary key basically.
Diffstat (limited to 'db/migrate/20180213131630_add_partial_index_to_projects_for_index_only_scans.rb')
-rw-r--r--db/migrate/20180213131630_add_partial_index_to_projects_for_index_only_scans.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/db/migrate/20180213131630_add_partial_index_to_projects_for_index_only_scans.rb b/db/migrate/20180213131630_add_partial_index_to_projects_for_index_only_scans.rb
new file mode 100644
index 00000000000..c8ebabbe627
--- /dev/null
+++ b/db/migrate/20180213131630_add_partial_index_to_projects_for_index_only_scans.rb
@@ -0,0 +1,21 @@
+class AddPartialIndexToProjectsForIndexOnlyScans < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+ INDEX_NAME = 'index_projects_on_id_partial_for_visibility'
+
+ disable_ddl_transaction!
+
+ # Adds a partial index to leverage index-only scans when looking up project ids
+ def up
+ unless index_exists?(:projects, :id, name: INDEX_NAME)
+ add_concurrent_index :projects, :id, name: INDEX_NAME, unique: true, where: 'visibility_level IN (10,20)'
+ end
+ end
+
+ def down
+ if index_exists?(:projects, :id, name: INDEX_NAME)
+ remove_concurrent_index :projects, :id, name: INDEX_NAME, unique: true, where: 'visibility_level IN (10,20)'
+ end
+ end
+end