Project

General

Profile

Actions

Defect #44724

closed

Issue search with searchable custom fields is slow on MariaDB when searching within a project

Defect #44724: Issue search with searchable custom fields is slow on MariaDB when searching within a project

Added by Francisco Torres García 25 days ago. Updated 20 days ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Performance
Target version:
Resolution:
Fixed
Affected version:

Description

Environment: Redmine 7.0.2, Rails 8.1.4, Ruby 4.0.9, MariaDB 11.8.8, adapter mysql2.
Data: ~112k issues, ~449k custom_values, ~6.3k enabled_modules, 9 searchable issue custom fields.

Problem

Searching text inside a project with many issues takes 36-131 s (almost all in ActiveRecord), so it times out behind a reverse proxy. Global search ("all projects") and small projects are fast (< 1.5 s).

Analysis

  1. The fix of #43651 (JOIN_ORDER(issues, custom_values) in search_result_ranks_and_ids) is only applied when ActiveRecord::Base.connection.supports_optimizer_hints? is true. Rails returns false for any MariaDB (!mariadb? && database_version >= "5.7.7"), so on MariaDB the hint is never added. Besides, MariaDB only supports new-style optimizer hints since 12.0.
  2. In search_scope, the following line produces issues.project_id IN ('123'), because Rails quotes numeric bind values as strings on MySQL adapters:
    scope = scope.where("#{searchable_options[:project_key]} IN (?)", projects.map(&:id))
    

    With the quoted value, MariaDB 11.8 does not propagate the constant to projects.id. The plan starts with a full scan of enabled_modules and repeats the issues/custom_values lookup for every project.
  3. The same query with IN (123), unquoted, starts with projects (const) and runs in 0.4 s instead of 131 s.

Workaround applied locally (lib/plugins/acts_as_searchable/lib/acts_as_searchable.rb, search_scope):

scope = scope.where("#{searchable_options[:project_key]} IN (#{projects.map { |p| p.id.to_i }.join(",")})")

With this change the same search takes about 0.5 s and returns the same results.

Passing integer ids unquoted, or an equivalent fix for MariaDB, would avoid the regression on MariaDB, where the optimizer-hint approach cannot be used. I can provide the full EXPLAIN output if useful.

Related to #43651.


Files


Related issues 1 (0 open — 1 closed)

Related to Redmine - Defect #43651: Searching issues with searchable custom fields causes a performance regression on MySQLClosedGo MAEDAActions

Updated by Go MAEDA 25 days ago Actions #1

  • Related to Defect #43651: Searching issues with searchable custom fields causes a performance regression on MySQL added

Updated by Go MAEDA 25 days ago Actions #2

  • Status changed from New to Confirmed

Updated by Go MAEDA 24 days ago Actions #3

Thank you for the detailed report and analysis.

I reproduced the issue on MariaDB 11.8.9 with a dataset of similar size (112,000 issues, 448,000 custom values, 700 projects, and 9 searchable issue custom fields). Searching within a project with 30,000 issues took about 1.9 seconds, and the query on custom_values used the plan you described.

I have attached a patch based on the workaround proposed by Francisco Torres García. Instead of embedding the IDs in the SQL string, the patch uses a hash condition:

scope = scope.where(searchable_options[:project_key] => projects.map(&:id))

Because Rails knows the column type for a hash condition, the IDs are passed as integers even on MySQL adapters. With this patch, the same search took about 0.2 seconds.

The change has no practical effect on PostgreSQL and SQLite, where the IDs were already passed as integers.

Updated by Go MAEDA 21 days ago Actions #4

  • Subject changed from Issue search with searchable custom fields is still very slow on MariaDB (fix of #43651 is never applied) to Issue search with searchable custom fields is slow on MariaDB when searching within a project
  • Status changed from Confirmed to Resolved
  • Assignee set to Go MAEDA
  • Resolution set to Fixed

Committed the fix in r25450. Thank you for reporting this issue.

Updated by Go MAEDA 20 days ago Actions #5

  • Status changed from Resolved to Closed

Merged the fix into the stable branches in r25451 and r25452.

Actions

Also available in: PDF Atom