Optimize hosts controller query: use pluck instead of map for hostgroup_ids#10951
Optimize hosts controller query: use pluck instead of map for hostgroup_ids#10951raman1236 wants to merge 1 commit into
Conversation
ofedoren
left a comment
There was a problem hiding this comment.
Thanks, @raman1236, it'll need a ticket (https://projects.theforeman.org/projects/foreman/issues/new) and then updated commit message with the ticket's number. Otherwise LGTM.
Does it though? |
|
@adamruzicka Great catch — you're absolutely right! I tested this myself and confirmed that Rails' |
|
Friendly ping — review feedback has been addressed (reverted the user.rb change, kept only the pluck optimization). The PR now focuses on a single targeted optimization in the hosts controller. Would appreciate another look. Thanks! |
LOL. |
|
@raman1236, please squash the commits into one and change the commit message to |
e2f2531 to
eb2899e
Compare
…map for hostgroup_ids
eb2899e to
499a3b5
Compare
Description
Optimize database query in hosts controller to reduce unnecessary memory allocation.
Change
app/controllers/hosts_controller.rb- Usepluckinstead ofmapmap(&:hostgroup_id)loads all Host ActiveRecord objects into memory and extracts the attribute in Ruby.pluck(:hostgroup_id)generates aSELECT hostgroup_id FROM ...query that returns only the needed column values directly from the database, avoiding unnecessary object instantiation. This is particularly impactful on the hosts index page which can display many hosts.Note on previous user.rb change
The original PR also included a
.where().any?→.exists?()change inuser.rb. As correctly pointed out by @adamruzicka, Rails already optimizes.any?to generate the same efficientSELECT 1 AS one ... LIMIT 1query, so that change was reverted.