Skip to content

Commit

Permalink
Avoid N+1 in get_selected_hosts
Browse files Browse the repository at this point in the history
`.load_ar_obj` will defer to `.load_ar_objs` when it is passed an array,
which will do a `.collect` call to `load_ar_obj` in return.  This is a
typical N+1, so this change takes the constantize code from
`.load_ar_obj` and applies it to the `allowed_hosts` to make a single
SQL call to for the needed hosts.
  • Loading branch information
NickLaMuro committed Apr 26, 2018
1 parent b77f060 commit d986cb6
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/models/miq_provision_virt_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,9 @@ def get_selected_hosts(src)
elsif src[:host_id]
selected_host(src)
else
load_ar_obj(allowed_hosts)
allowed_hosts.group_by(&:evm_object_class).flat_map do |type, objs|
type.to_s.camelize.constantize.where(:id => objs.map(&:id)).to_a
end
end
Rbac.filtered(hosts, :class => Host, :user => @requester)
end
Expand Down

0 comments on commit d986cb6

Please sign in to comment.