Skip to content

Commit

Permalink
Merge pull request #17354 from NickLaMuro/sql_improvements_for_miq_re…
Browse files Browse the repository at this point in the history
…quest_workflow

Prevents N+1 SQL queries in miq_request_workflow.rb
  • Loading branch information
kbrock authored May 18, 2018
2 parents 6480221 + d986cb6 commit 65631b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
12 changes: 10 additions & 2 deletions app/models/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1750,11 +1750,19 @@ def openstack_host? # TODO: This doesn't belong here
end

def writable_storages
storages.where(:host_storages => {:read_only => [false, nil]})
if host_storages.loaded? && host_storages.all? { |hs| hs.association(:storage).loaded? }
host_storages.reject(&:read_only).map(&:storage)
else
storages.where(:host_storages => {:read_only => [false, nil]})
end
end

def read_only_storages
storages.where(:host_storages => {:read_only => true})
if host_storages.loaded? && host_storages.all? { |hs| hs.association(:storage).loaded? }
host_storages.select(&:read_only).map(&:storage)
else
storages.where(:host_storages => {:read_only => true})
end
end

def archived?
Expand Down
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
2 changes: 1 addition & 1 deletion app/models/miq_request_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ def allowed_storages(_options = {})

rails_logger('allowed_storages', 0)
st = Time.now
MiqPreloader.preload(hosts, :storages)
MiqPreloader.preload(hosts, :storages => {}, :host_storages => :storage)

storages = hosts.each_with_object({}) do |host, hash|
host.writable_storages.each { |s| hash[s.id] = s }
Expand Down

0 comments on commit 65631b8

Please sign in to comment.