Project

General

Profile

Defect #43651 ยป fix-custom-value-search-degraded-performance.patch

Go MAEDA, 2026-01-07 08:20

View differences:

lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb
109 109
                    clauses << "(#{CustomValue.table_name}.custom_field_id IN (#{fields.map(&:id).join(',')}) AND (#{visibility}))"
110 110
                  end
111 111
                  visibility = clauses.join(' OR ')
112
                  r |= fetch_ranks_and_ids(
113
                    search_scope(user, projects, options).
114
                    joins(:custom_values).
115
                    where(visibility).
116
                    where(search_tokens_condition(["#{CustomValue.table_name}.value"], tokens, options[:all_words])),
117
                    options[:limit]
118
                  )
112
                  scope =
113
                    search_scope(user, projects, options)
114
                    .joins(:custom_values)
115
                    .where(visibility)
116
                    .where(search_tokens_condition(["#{CustomValue.table_name}.value"], tokens, options[:all_words]))
117

  
118
                  if self == Issue && Redmine::Database.mysql? && ActiveRecord::Base.connection.supports_optimizer_hints?
119
                    # Force the join order to evaluate issues before custom_values.
120
                    # This avoids inefficient query plans where custom_values are scanned
121
                    # without an issue_id filter, which can be extremely slow on large datasets.
122
                    scope = scope.optimizer_hints("JOIN_ORDER(#{Issue.table_name}, #{CustomValue.table_name})")
123
                  end
124

  
125
                  r |= fetch_ranks_and_ids(scope, options[:limit])
119 126
                  queries += 1
120 127
                end
121 128
              end
    (1-1/1)