Project

General

Profile

Actions

Patch #40798

closed

Optimize Version model

Added by Go MAEDA 7 months ago. Updated 6 months ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Performance
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:

Description

The attached patch improves the performance of several methods in the Version model such as Version#issues_count, FixedIssuesExtension#completed_percent, FixedIssuesExtension#closed_percent, and FixedIssuesExtension#issues_progress.

These methods use count method to get the total number of fixed issues in the version. It performs an SQL count query every time it is called. This patch replaces count with open_count + closed_count in order to use cached values instead of performing SQL count queries.

The change has an effect on Gantt as well as Roadmap.


Files

Actions #1

Updated by Go MAEDA 7 months ago

The following are SQL queries triggered by Version#behind_schedule?.

Before:

irb(main):001> Version.find(2).behind_schedule?
  Version Load (0.1ms)  SELECT "versions".* FROM "versions" WHERE "versions"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  Issue Count (0.1ms)  SELECT COUNT(*) FROM "issues" WHERE "issues"."fixed_version_id" = ?  [["fixed_version_id", 2]]
  Issue Count (0.1ms)  SELECT COUNT(*) AS "count_all", "issues"."status_id" AS "issues_status_id" FROM "issues" WHERE "issues"."fixed_version_id" = ? GROUP BY "issues"."status_id"  [["fixed_version_id", 2]]
  IssueStatus Load (0.1ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" IN (?, ?)  [["id", 2], ["id", 5]]
  Issue Count (0.1ms)  SELECT COUNT(*) FROM "issues" WHERE "issues"."fixed_version_id" = ?  [["fixed_version_id", 2]]
  Issue Load (0.2ms)  SELECT "issues".* FROM "issues" INNER JOIN "issue_statuses" ON "issue_statuses"."id" = "issues"."status_id" WHERE "issues"."fixed_version_id" = ? AND "issue_statuses"."is_closed" = ?  [["fixed_version_id", 2], ["is_closed", 1]]
  Issue Load (0.1ms)  SELECT "issues".* FROM "issues" WHERE "issues"."fixed_version_id" = ?  [["fixed_version_id", 2]]
  IssueStatus Load (0.1ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 5], ["LIMIT", 1]]
  Issue Count (0.0ms)  SELECT COUNT(*) FROM "issues" WHERE "issues"."fixed_version_id" = ?  [["fixed_version_id", 2]]
  Issue Count (0.0ms)  SELECT COUNT(*) FROM "issues" WHERE "issues"."fixed_version_id" = ?  [["fixed_version_id", 2]]
  Issue Load (0.0ms)  SELECT "issues".* FROM "issues" INNER JOIN "issue_statuses" ON "issue_statuses"."id" = "issues"."status_id" WHERE "issues"."fixed_version_id" = ? AND "issue_statuses"."is_closed" = ?  [["fixed_version_id", 2], ["is_closed", 0]]
  IssueStatus Load (0.0ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  Setting Load (0.0ms)  SELECT "settings".* FROM "settings" WHERE "settings"."name" = ? ORDER BY "settings"."id" DESC LIMIT ?  [["name", "issue_done_ratio"], ["LIMIT", 1]]
  Issue Count (0.0ms)  SELECT COUNT(*) FROM "issues" WHERE "issues"."fixed_version_id" = ?  [["fixed_version_id", 2]]
  Issue Minimum (0.0ms)  SELECT MIN("issues"."start_date") FROM "issues" WHERE "issues"."fixed_version_id" = ?  [["fixed_version_id", 2]]
  Issue Count (0.0ms)  SELECT COUNT(*) FROM "issues" WHERE "issues"."fixed_version_id" = ?  [["fixed_version_id", 2]]
  AnonymousUser Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."type" = ? AND "users"."lastname" = ? LIMIT ?  [["type", "AnonymousUser"], ["lastname", "Anonymous"], ["LIMIT", 1]]
=> false

After:

irb(main):001> Version.find(2).behind_schedule?
  Version Load (0.1ms)  SELECT "versions".* FROM "versions" WHERE "versions"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  Issue Count (0.1ms)  SELECT COUNT(*) AS "count_all", "issues"."status_id" AS "issues_status_id" FROM "issues" WHERE "issues"."fixed_version_id" = ? GROUP BY "issues"."status_id"  [["fixed_version_id", 2]]
  IssueStatus Load (0.1ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" IN (?, ?)  [["id", 2], ["id", 5]]
  Issue Load (0.1ms)  SELECT "issues".* FROM "issues" INNER JOIN "issue_statuses" ON "issue_statuses"."id" = "issues"."status_id" WHERE "issues"."fixed_version_id" = ? AND "issue_statuses"."is_closed" = ?  [["fixed_version_id", 2], ["is_closed", 1]]
  Issue Load (0.5ms)  SELECT "issues".* FROM "issues" WHERE "issues"."fixed_version_id" = ?  [["fixed_version_id", 2]]
  IssueStatus Load (0.0ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 5], ["LIMIT", 1]]
  Issue Load (0.0ms)  SELECT "issues".* FROM "issues" INNER JOIN "issue_statuses" ON "issue_statuses"."id" = "issues"."status_id" WHERE "issues"."fixed_version_id" = ? AND "issue_statuses"."is_closed" = ?  [["fixed_version_id", 2], ["is_closed", 0]]
  IssueStatus Load (0.0ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  Setting Load (0.0ms)  SELECT "settings".* FROM "settings" WHERE "settings"."name" = ? ORDER BY "settings"."id" DESC LIMIT ?  [["name", "issue_done_ratio"], ["LIMIT", 1]]
  Issue Minimum (0.1ms)  SELECT MIN("issues"."start_date") FROM "issues" WHERE "issues"."fixed_version_id" = ?  [["fixed_version_id", 2]]
  AnonymousUser Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."type" = ? AND "users"."lastname" = ? LIMIT ?  [["type", "AnonymousUser"], ["lastname", "Anonymous"], ["LIMIT", 1]]
=> false

Actions #3

Updated by Go MAEDA 7 months ago

  • File deleted (0001-Improve-performance-of-Version-model-by-reducing-red.patch)
Actions #4

Updated by Go MAEDA 7 months ago

  • File deleted (0002-Optimize-FixedIssuesExtension-closed_percent-by-bypa.patch)
Actions #5

Updated by Go MAEDA 6 months ago

  • Target version changed from Candidate for next major release to 6.0.0

Setting the target version to 6.0.0.

Actions #6

Updated by Go MAEDA 6 months ago

  • Subject changed from Reduce redundant SQL count queries in Version model to Optimize Version model
  • Status changed from New to Closed
  • Assignee set to Go MAEDA

Committed the patches in r22880, r22881, and r22882.

Actions

Also available in: Atom PDF