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 involving_user scope which improves date alert creation performance

ulfertsa4a53dec9126
diff --git a/app/models/work_packages/scopes/involving_user.rb b/app/models/work_packages/scopes/involving_user.rb
index da00ea9a04b..0eb82d7131d 100644
--- a/app/models/work_packages/scopes/involving_user.rb
+++ b/app/models/work_packages/scopes/involving_user.rb
@@ -36,19 +36,45 @@ module WorkPackages::Scopes
# for, or watcher, via a group or themself
#
# @param user User the user involved in work packages.
- def involving_user(user)
- WorkPackage.left_joins(:watchers)
- .where(watchers: { user: })
- .or(WorkPackage.where(assigned_to: user))
- .or(WorkPackage.where(assigned_to: group_having(user)))
- .or(WorkPackage.where(responsible: user))
- .or(WorkPackage.where(responsible: group_having(user)))
+ def involving_user(user) # rubocop:disable Metrics/AbcSize
+ # Using CTEs and UNION for performance reasons
+ # The alternative would be the simpler:
+ # ```
+ # WorkPackage
+ # .left_joins(:watchers)
+ # .where(watchers: { user: })
+ # .or(WorkPackage.where(assigned_to: user))
+ # .or(WorkPackage.where(assigned_to: group_having(user)))
+ # .or(WorkPackage.where(responsible: user))
+ # .or(WorkPackage.where(responsible: group_having(user)))
+ # ```
+ # That however prevents indices from being used because of the or clauses on different columns.
+ watched = WorkPackage
+ .left_joins(:watchers)
+ .where(watchers: { user: })
+ .select(:id)
+ assigned = WorkPackage
+ .where("assigned_to_id IN (SELECT * FROM user_and_groups)")
+ .select(:id)
+ responsible = WorkPackage
+ .where("responsible_id IN (SELECT * FROM user_and_groups)")
+ .select(:id)
+
+ watched_and_assigned_and_responsible = Arel::Nodes::Union.new(watched.arel,
+ Arel::Nodes::Union.new(assigned.arel,
+ responsible.arel))
+
+ WorkPackage
+ .with(user_and_groups: user_and_groups_union(user))
+ .where(arel_table[:id].in(watched_and_assigned_and_responsible))
end
private
- def group_having(user)
- GroupUser.select(:group_id).where(user_id: user.id)
+ def user_and_groups_union(user)
+ in_group = GroupUser.select("group_id").where(["user_id = ?", user.id]).arel # rubocop:disable Rails/WhereEquals
+
+ Arel.sql("#{in_group.to_sql} UNION SELECT #{user.id}")
end
end
end
diff --git a/app/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages.rb b/app/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages.rb
index abb42793c2c..9c6319830af 100644
--- a/app/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages.rb
+++ b/app/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages.rb
@@ -113,8 +113,8 @@ class Notifications::CreateDateAlertsNotificationsJob::AlertableWorkPackages
def alertable_work_packages
work_packages = WorkPackage
- .with_status_open
- .involving_user(user)
+ .involving_user(user)
+ .with_status_open
# `work_packages.to_sql` was producing SQL with weird select clauses that could
# not be used in a CTE, while doing `work_packages.pluck(:something)` was producing