Project

General

Profile

Actions

Patch #38474

closed

Preload default_status when listing trackers

Added by Go MAEDA over 1 year ago. Updated over 1 year ago.

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

0%

Estimated time:

Description

The patch fixes N+1 issue in TrackersController#index.

diff --git a/app/controllers/trackers_controller.rb b/app/controllers/trackers_controller.rb
index 64367f373..05ef336ef 100644
--- a/app/controllers/trackers_controller.rb
+++ b/app/controllers/trackers_controller.rb
@@ -26,7 +26,7 @@ class TrackersController < ApplicationController
   accept_api_auth :index

   def index
-    @trackers = Tracker.sorted.to_a
+    @trackers = Tracker.sorted.preload(:default_status).to_a
     respond_to do |format|
       format.html {render :layout => false if request.xhr?}
       format.api

Before:

  Tracker Load (0.1ms)  SELECT "trackers".* FROM "trackers" ORDER BY "trackers"."position" ASC
  ↳ app/controllers/trackers_controller.rb:29:in `index'
  Rendering trackers/index.api.rsb
  IssueStatus Load (0.1ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/views/trackers/index.api.rsb:6
  IssueStatus Load (0.1ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ app/views/trackers/index.api.rsb:6
  CACHE IssueStatus Load (0.0ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/views/trackers/index.api.rsb:6
  CACHE IssueStatus Load (0.0ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/views/trackers/index.api.rsb:6
  CACHE IssueStatus Load (0.0ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ app/views/trackers/index.api.rsb:6
  IssueStatus Load (0.1ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 5], ["LIMIT", 1]]
  ↳ app/views/trackers/index.api.rsb:6
  IssueStatus Load (0.1ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
  ↳ app/views/trackers/index.api.rsb:6

After:

  Tracker Load (0.2ms)  SELECT "trackers".* FROM "trackers" ORDER BY "trackers"."position" ASC
  ↳ app/controllers/trackers_controller.rb:29:in `index'
  IssueStatus Load (0.6ms)  SELECT "issue_statuses".* FROM "issue_statuses" WHERE "issue_statuses"."id" IN (?, ?, ?, ?)  [["id", 1], ["id", 2], ["id", 5], ["id", 3]]
  ↳ app/controllers/trackers_controller.rb:29:in `index'

Actions

Also available in: Atom PDF