Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

improve performance of WorkPackageCustomField.visible_by_user

ulfertsaf6fc5518128
diff --git a/app/models/work_package_custom_field.rb b/app/models/work_package_custom_field.rb
index c41c2553ce1..f2904d7f5bc 100644
--- a/app/models/work_package_custom_field.rb
+++ b/app/models/work_package_custom_field.rb
@@ -47,10 +47,15 @@ class WorkPackageCustomField < CustomField
}
scope :visible_by_user, ->(user) {
- where(projects: { id: Project.visible(user) })
- .where(types: { id: Type.enabled_in(Project.visible(user)) })
- .or(where(is_for_all: true).references(:projects, :types))
- .includes(:projects, :types)
+ # Prefer a subquery to a join to avoid the database query returning
+ # the cross product of projects, types and custom fields.
+ where(id:
+ unscoped
+ .where(projects: { id: Project.visible(user) })
+ .where(types: { id: Type.enabled_in(Project.visible(user)) })
+ .or(unscoped.where(is_for_all: true))
+ .includes(:projects, :types)
+ .select(:id))
}
scope :usable_as_custom_action, -> {