From: Marius Balteanu Date: Fri, 23 Sep 2022 05:51:59 +0000 (+0000) Subject: Fix perfomance issue when objects are filtered by custom field values (#37268). X-Git-Tag: 5.1.0~422 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a059599a8bcf69e98af3ee353ce272c0721ed804;p=redmine.git Fix perfomance issue when objects are filtered by custom field values (#37268). git-svn-id: https://svn.redmine.org/redmine/trunk@21833 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/query.rb b/app/models/query.rb index 196568379..9f7de57c7 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -1160,12 +1160,13 @@ class Query < ActiveRecord::Base if /[<>]/.match?(operator) where = "(#{where}) AND #{db_table}.#{db_field} <> ''" end - "#{queried_table_name}.#{customized_key} #{not_in} IN (" \ - "SELECT #{customized_class.table_name}.id FROM #{customized_class.table_name}" \ + "#{not_in} EXISTS (" \ + "SELECT ct.id FROM #{customized_class.table_name} ct" \ " LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='#{customized_class}'" \ - " AND #{db_table}.customized_id=#{customized_class.table_name}.id" \ + " AND #{db_table}.customized_id=ct.id" \ " AND #{db_table}.custom_field_id=#{custom_field_id}" \ - " WHERE (#{where}) AND (#{filter[:field].visibility_by_project_condition}))" + " WHERE #{queried_table_name}.#{customized_key} = ct.id AND " \ + " (#{where}) AND (#{filter[:field].visibility_by_project_condition}))" end def sql_for_chained_custom_field(field, operator, value, custom_field_id, chained_custom_field_id)