From 09d354d638b8beed50a06dcffc2233d973238e0b Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 17 Jul 2017 17:23:07 -0400 Subject: [PATCH 1/2] Reduce MiqGroup#seed queries assigning miq_user_role was causing a bunch of sub queries --- app/models/miq_group.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/models/miq_group.rb b/app/models/miq_group.rb index b83e4b1ed25..a40a475b1c3 100644 --- a/app/models/miq_group.rb +++ b/app/models/miq_group.rb @@ -60,15 +60,18 @@ def self.seed ldap_to_filters = filter_map_file.exist? ? YAML.load_file(filter_map_file) : {} root_tenant = Tenant.root_tenant + groups = where(:group_type => SYSTEM_GROUP).includes(:entitlement).index_by(&:description) + roles = MiqUserRole.where("name like 'EvmRole-%'").index_by(&:name) + role_map.each_with_index do |(group_name, role_name), index| - group = find_by(:description => group_name) || new(:description => group_name) - user_role = MiqUserRole.find_by(:name => "EvmRole-#{role_name}") + group = groups[group_name] || new(:description => group_name) + user_role = roles["EvmRole-#{role_name}"] if user_role.nil? raise StandardError, _("Unable to find user_role 'EvmRole-%{role_name}' for group '%{group_name}'") % {:role_name => role_name, :group_name => group_name} end - group.miq_user_role = user_role + group.miq_user_role = user_role if group.entitlement.try(:miq_user_role_id) != user_role.id group.sequence = index + 1 group.entitlement.filters = ldap_to_filters[group_name] group.group_type = SYSTEM_GROUP From a7e65184b93b1fd154959b5e587c74e3abc3a84b Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 17 Jul 2017 17:41:15 -0400 Subject: [PATCH 2/2] Reduce ServerRole#seed queries Simple N+1 fix --- app/models/server_role.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/server_role.rb b/app/models/server_role.rb index 4c0a5ce16c5..08d58c5968f 100644 --- a/app/models/server_role.rb +++ b/app/models/server_role.rb @@ -6,10 +6,11 @@ class ServerRole < ApplicationRecord validates_uniqueness_of :name def self.seed + server_roles = all.index_by(&:name) CSV.foreach(fixture_path, :headers => true, :skip_lines => /^#/).each do |csv_row| action = csv_row.to_hash - rec = find_by(:name => action['name']) + rec = server_roles[action['name']] if rec.nil? _log.info("Creating Server Role [#{action['name']}]") create(action)