Project

General

Profile

Actions

Defect #44811

open

Gantt chart and roadmap repeat the same SUM query for every parent issue of a version

Defect #44811: Gantt chart and roadmap repeat the same SUM query for every parent issue of a version

Added by Yukke KAWAI 2 days ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
Gantt
Target version:
-
Resolution:
Affected version:

Description

For versions that contain many parent issues, the gantt chart and the roadmap run thousands of queries. Most of them are the same SUM(estimated_hours) query, repeated for every parent issue of every version, and again for each place that asks for the version's progress. On our data, the default gantt chart of one project takes 30.1 s and 4,301 queries, 3,665 of which are this SUM. Loading the same data in batches brings it to 8.0 s and 159 queries, with the same HTML.

Environment

  • Redmine 7.0 (official Docker image redmine:7.0), Ruby 4.0, Rails 8.1
  • PostgreSQL 17 (Aurora Serverless v2)
  • 0.25 vCPU / 1 GB (AWS Fargate, ARM64), one Puma process
  • Some plugins are installed. None of them appear in the backtraces of the queries described below; all of them come from core code.

The project has 34,563 issues, 675 of them open, and 56 shared versions. Many issues are parent issues (2 to 4 levels) and are assigned to versions.

Steps to reproduce

  1. Create a project with versions that have start and due dates.
  2. Assign a few hundred issues to the versions, many of them parent issues with subtasks.
  3. Open the gantt chart of the project with the default query (open issues).
  4. Open the roadmap of the project.

Measurements

Each value is one request by a non-admin member, measured with ActiveSupport::Notifications in the same process. "Before" is core; "after" loads the data in batches as proposed below.

Page vCPU Before After
Gantt chart (default query) 0.25 30.1 s, 4,301 queries, 6,267 cache hits 8.0 s, 159 queries, 259 cache hits
Gantt chart (default query) 1 8.5 s, 4,301 queries 1.8 s, 159 queries
Gantt chart (all statuses, 24 months) 0.25 11.1 s, 818 queries, 2,488 cache hits 7.3 s, 109 queries, 146 cache hits
Roadmap 0.25 19.8 s, 3,021 queries, 4,777 cache hits 3.2 s, 130 queries, 86 cache hits

The requests are CPU-bound: the default gantt chart used 7.2 s of CPU time at 0.25 vCPU. After the change, most of the remaining time is spent rendering the HTML of the chart (about 2 MB for 1,000 rows).

The HTML of the gantt chart and the roadmap was byte-for-byte the same before and after, for an anonymous, an admin and a member user. The only difference was the "Recently used" list of the project menu, whose order depends on the pages visited before.

Causes

Line numbers refer to master. The same code is in 7.0.

1. Version progress runs one SUM per parent issue, several times per version

Version#completed_percent, #closed_percent and #behind_schedule? call FixedIssuesExtension#issues_progress and #estimated_average (app/models/version.rb, lines 81 to 117).

  • estimated_average loads the issues of the version and calls Issue#total_estimated_hours on each one. For a parent issue this runs self_and_descendants.visible.sum(:estimated_hours) (app/models/issue.rb, line 1208).
  • issues_progress(open) then iterates over self.open(open) (line 106). This is a new relation, so the issues are loaded again as new objects without the memoized @total_estimated_hours, and every parent issue runs the same SUM again, once for the open issues and once for the closed ones.
  • On the gantt chart, each version row computes the progress for both version.fixed_issues (through behind_schedule?, lib/redmine/helpers/gantt.rb line 833) and version.visible_fixed_issues (lines 397 to 400 and 836 to 840). These are two relations with their own memoization, so the work is done twice.

Traced on our data, the 3,665 SUM queries come from these four call paths: estimated_average and issues_progress, each for fixed_issues and for visible_fixed_issues (1,093, 1,093, 1,083 and 1,083 queries).

2. Sorting the issues loads each ancestor with a query

Redmine::Helpers::Gantt.sort_issue_logic (lib/redmine/helpers/gantt.rb, line 787) walks up with current_issue.parent for every issue, which loads each ancestor with its own query. The issues are sorted again for each version and for each rendering pass (subjects, lines, selected columns). On our data this is 1,004 queries for the gantt chart, mostly served by the query cache.

3. Each parent issue loads its children with a query

Redmine::Helpers::Gantt#html_subject (line 869) calls object.children & project_issues(object.project) for each parent issue. children (lib/redmine/nested_set/traversing.rb) returns a new relation, so this is one query per parent issue and per rendering pass (292 queries on our data).

Proposed fix

The progress formula does not need to change. Only the loading does:

  • In FixedIssuesExtension, load the issues of the relation once with their status, and get total_estimated_hours of all parent issues with one grouped query that keeps the same visibility condition: join issues to itself on root_id, lft and rgt, apply Issue.visible, and group by the parent's id. Then split open and closed issues in memory instead of calling open(open).
  • In sort_issues!, load the missing ancestors of all issues one level at a time and build the same sort keys from a hash. Plain where(id: ...) queries keep the query cache working across versions and passes; find_each would bypass it.
  • In the gantt chart, load the children of all parent issues of the chart with one query, or replace object.children & project_issues(...) with a lookup of the chart's issues by parent_id, which gives the same result.

These changes are implemented as a plugin, with tests that compare the results with core in the same process, including a child issue in a private project: https://github.com/yukkes/redmine_gantt_speedup

If this approach is acceptable, I can prepare a patch against master with unit tests for Version and Redmine::Helpers::Gantt.

No data to display

Actions

Also available in: PDF Atom