Project

General

Profile

Actions

Defect #26691

closed

N + 1 queries when rendering the Gantt

Added by jwjw yy over 6 years ago. Updated about 5 years ago.

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

0%

Estimated time:
Resolution:
Wont fix
Affected version:

Description

Rails 4.2.7.1
20000 issues in the database
When rendering the GanttsController.show action, it will issue a lot of queries to get the the start_date and due_date of an project like:

SELECT MIN(`issues`.`start_date`) FROM `issues` WHERE `issues`.`project_id` = ?  [["project_id", 1436]]
SELECT MAX(`issues`.`due_date`) FROM `issues` WHERE `issues`.`project_id` = ?  [["project_id", 1436]]
SELECT MIN(`issues`.`start_date`) FROM `issues` WHERE `issues`.`project_id` = ?  [["project_id", 1317]]
SELECT MAX(`issues`.`due_date`) FROM `issues` WHERE `issues`.`project_id` = ?  [["project_id", 1317]]

Which will make the performance very bad.

My solution is that add cached_due_date and cached_start_date for projects.rb, and while constructing the project_tree, I will use a group query to retrieve the corresponding start_date and due_date for a group of projects, so that they don't need to issue N queries later on.


Files

project.rb (38.8 KB) project.rb jwjw yy, 2017-08-15 03:27
cache_start_due_date.rb.diff (1.97 KB) cache_start_due_date.rb.diff Toshi MARUYAMA, 2017-08-15 10:34
Actions

Also available in: Atom PDF