Project

General

Profile

Actions

Patch #26711

closed

Use pluck instead of collect/map

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

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

0%

Estimated time:

Description

In app/helpers/my_helpers,
68: where(:project_id => User.current.projects.map(&:id)).
Use pluck to get the id is much more efficient, since the query will only access the id field, instead of select * from projects:
Before:
SELECT `projects`.* FROM `projects` INNER JOIN `members` ON `projects`.`id` = `members`.`project_id` WHERE `members`.`user_id` = ? AND (projects.status<>9) "user_id", 5341

After:
SELECT `projects`.`id` FROM `projects` INNER JOIN `members` ON `projects`.`id` = `members`.`project_id` WHERE `members`.`user_id` = ? AND (projects.status<>9) "user_id", 5341


Related issues

Related to Redmine - Patch #26726: Use pluck(:id) instead of collect(&:id) Closed

Actions
Actions #1

Updated by Toshi MARUYAMA over 6 years ago

  • Category set to Code cleanup/refactoring
  • Target version set to 4.1.0
Actions #2

Updated by Toshi MARUYAMA over 6 years ago

  • Tracker changed from Defect to Patch
Actions #3

Updated by Go MAEDA over 6 years ago

Index: app/helpers/my_helper.rb
===================================================================
--- app/helpers/my_helper.rb    (revision 16952)
+++ app/helpers/my_helper.rb    (working copy)
@@ -78,7 +78,7 @@
   def render_calendar_block(block, settings)
     calendar = Redmine::Helpers::Calendar.new(User.current.today, current_language, :week)
     calendar.events = Issue.visible.
-      where(:project_id => User.current.projects.map(&:id)).
+      where(:project_id => User.current.projects.pluck(:id)).
       where("(start_date>=? and start_date<=?) or (due_date>=? and due_date<=?)", calendar.startdt, calendar.enddt, calendar.startdt, calendar.enddt).
       includes(:project, :tracker, :priority, :assigned_to).
       references(:project, :tracker, :priority, :assigned_to).
@@ -139,7 +139,7 @@

   def render_news_block(block, settings)
     news = News.visible.
-      where(:project_id => User.current.projects.map(&:id)).
+      where(:project_id => User.current.projects.pluck(:id)).
       limit(10).
       includes(:project, :author).
       references(:project, :author).
Actions #4

Updated by Go MAEDA over 6 years ago

  • Subject changed from .map should be changed to .pluck to Use pluck instead of map in MyHelper
  • Status changed from New to Closed
  • Assignee set to Go MAEDA
  • Target version changed from 4.1.0 to 4.0.0

Commited in the trunk. Thank you for pointing it out.

Actions #5

Updated by Go MAEDA over 5 years ago

  • Subject changed from Use pluck instead of map in MyHelper to Use pluck instead of collect/map
  • Category changed from Code cleanup/refactoring to Performance
Actions #6

Updated by Go MAEDA over 5 years ago

  • Related to Patch #26726: Use pluck(:id) instead of collect(&:id) added
Actions

Also available in: Atom PDF