summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/reindexing/index_selection.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/database/reindexing/index_selection.rb')
-rw-r--r--lib/gitlab/database/reindexing/index_selection.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/gitlab/database/reindexing/index_selection.rb b/lib/gitlab/database/reindexing/index_selection.rb
new file mode 100644
index 00000000000..406e70791df
--- /dev/null
+++ b/lib/gitlab/database/reindexing/index_selection.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module Database
+ module Reindexing
+ class IndexSelection
+ include Enumerable
+
+ delegate :each, to: :indexes
+
+ def initialize(candidates)
+ @candidates = candidates
+ end
+
+ private
+
+ attr_reader :candidates
+
+ def indexes
+ # This is an explicit N+1 query:
+ # Bloat estimates are generally available through a view
+ # for all indexes. However, estimating bloat for all
+ # indexes at once is an expensive operation. Therefore,
+ # we force a N+1 pattern here and estimate bloat on a per-index
+ # basis.
+
+ @indexes ||= filter_candidates.sort_by(&:bloat_size).reverse
+ end
+
+ def filter_candidates
+ candidates.not_recently_reindexed
+ end
+ end
+ end
+ end
+end