Vote #78761
完了Use pluck instead of collect/map
0%
説明
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
journals
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
<pre><code class="diff">
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).
</code></pre>
--------------------------------------------------------------------------------
Commited in the trunk. Thank you for pointing it out.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
related_issues
relates,Closed,26726,Use pluck(:id) instead of collect(&:id)