Patch #26711

Use pluck instead of collect/map

Added by jwjw yy 12 months ago. Updated 17 days ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Go MAEDA% Done:

0%

Category:Performance
Target version:4.0.0

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

Associated revisions

Revision 17036
Added by Go MAEDA 9 months ago

Use pluck instead of map in MyHelper (#26711).

Patch by jwjw yy.

Revision 17454
Added by Go MAEDA 17 days ago

Use pluck(:id) instead of collect(&:id) (#26711).

Patch by jwjw yy.

Revision 17455
Added by Go MAEDA 16 days ago

Use pluck(:id) instead of collect(&:id) in app/models/issue.rb (#26711).

History

#1 Updated by Toshi MARUYAMA 12 months ago

  • Category set to Code cleanup/refactoring
  • Target version set to 4.1.0

#2 Updated by Toshi MARUYAMA 12 months ago

  • Tracker changed from Defect to Patch

#3 Updated by Go MAEDA 12 months 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).

#4 Updated by Go MAEDA 9 months 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.

#5 Updated by Go MAEDA 17 days 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

#6 Updated by Go MAEDA 17 days ago

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

Also available in: Atom PDF