Skip to content

Commit

Permalink
Fixes #37842 - Fix duplicate host ids in host list query.
Browse files Browse the repository at this point in the history
Database server consumes high memory and long time to process
a query with huge numbers of duplicate host ids. This commit
fixes the issue.

(cherry picked from commit 3c8c351)
  • Loading branch information
hao-yu authored and chris1984 committed Dec 3, 2024
1 parent a0ca4f2 commit 3975aa3
Showing 1 changed file with 12 additions and 36 deletions.
48 changes: 12 additions & 36 deletions app/models/katello/concerns/content_facet_host_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,14 @@ def content_facet_ignore_update?(attributes)
module ClassMethods
def find_by_applicable_errata(_key, operator, value)
conditions = sanitize_sql_for_conditions(["#{Katello::Erratum.table_name}.errata_id #{operator} ?", value_to_sql(operator, value)])
hosts = ::Host::Managed.joins(:applicable_errata).where(conditions)
if hosts.empty?
{ :conditions => "1=0" }
else
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{hosts.pluck(:id).join(',')})" }
end
hosts = ::Host::Managed.joins(:applicable_errata).select(:id).where(conditions)
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{hosts.to_sql})" }
end

def find_by_installable_errata(_key, operator, value)
conditions = sanitize_sql_for_conditions(["#{Katello::Erratum.table_name}.errata_id #{operator} ?", value_to_sql(operator, value)])
facets = Katello::Host::ContentFacet.joins_installable_errata.where(conditions)
if facets.empty?
{ :conditions => "1=0" }
else
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.pluck(:host_id).join(',')})" }
end
facets = Katello::Host::ContentFacet.joins_installable_errata.select(:host_id).where(conditions)
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.to_sql})" }
end

def find_by_applicable_debs(_key, operator, value)
Expand All @@ -117,42 +109,26 @@ def find_by_installable_debs(_key, operator, value)

def find_by_applicable_rpms(_key, operator, value)
conditions = sanitize_sql_for_conditions(["#{Katello::Rpm.table_name}.nvra #{operator} ?", value_to_sql(operator, value)])
hosts = ::Host::Managed.joins(:applicable_rpms).where(conditions)
if hosts.empty?
{ :conditions => "1=0" }
else
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{hosts.pluck(:id).join(',')})" }
end
hosts = ::Host::Managed.joins(:applicable_rpms).select(:id).where(conditions)
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{hosts.to_sql})" }
end

def find_by_installable_rpms(_key, operator, value)
conditions = sanitize_sql_for_conditions(["#{Katello::Rpm.table_name}.nvra #{operator} ?", value_to_sql(operator, value)])
facets = Katello::Host::ContentFacet.joins_installable_rpms.where(conditions)
if facets.empty?
{ :conditions => "1=0" }
else
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.pluck(:host_id).join(',')})" }
end
facets = Katello::Host::ContentFacet.joins_installable_rpms.select(:host_id).where(conditions)
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.to_sql})" }
end

def find_by_repository_content_label(_key, operator, value)
conditions = sanitize_sql_for_conditions(["#{Katello::Content.table_name}.label #{operator} ?", value_to_sql(operator, value)])
facets = Katello::Host::ContentFacet.joins_repositories.where(conditions)
if facets.empty?
{ :conditions => "1=0" }
else
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.pluck(:host_id).join(',')})" }
end
facets = Katello::Host::ContentFacet.joins_repositories.select(:host_id).where(conditions)
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.to_sql})" }
end

def find_by_repository_name(_key, operator, value)
conditions = sanitize_sql_for_conditions(["#{Katello::RootRepository.table_name}.name #{operator} ?", value_to_sql(operator, value)])
facets = Katello::Host::ContentFacet.joins_repositories.where(conditions)
if facets.empty?
{ :conditions => "1=0" }
else
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.pluck(:host_id).join(',')})" }
end
facets = Katello::Host::ContentFacet.joins_repositories.select(:host_id).where(conditions)
{ :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.to_sql})" }
end

def in_content_view_environments(content_views: nil, lifecycle_environments: nil)
Expand Down

0 comments on commit 3975aa3

Please sign in to comment.