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

speedup sql for alertable work packages

ulferts55d1ccc12140
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 fb3db9ef7f6..b547b4ddc53 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
@@ -55,7 +55,6 @@ class Notifications::CreateDateAlertsNotificationsJob::AlertableWorkPackages
def query
today = Arel::Nodes::build_quoted(Date.current).to_sql
- alertable_durations = Arel::Nodes::Grouping.new(UserPreferences::ParamsContract::DATE_ALERT_DURATIONS.compact).to_sql
alertables = alertable_work_packages
.select(:id,
@@ -63,9 +62,9 @@ class Notifications::CreateDateAlertsNotificationsJob::AlertableWorkPackages
"work_packages.start_date - #{today} AS start_delta",
"work_packages.due_date - #{today} AS due_delta",
"#{today} - work_packages.due_date AS overdue_delta")
- .where("work_packages.start_date - #{today} IN #{alertable_durations} " \
- "OR work_packages.due_date - #{today} IN #{alertable_durations} " \
- "OR #{today} - work_packages.due_date > 0")
+ .where("work_packages.start_date IN #{alertable_dates} " \
+ "OR work_packages.due_date IN #{alertable_dates} " \
+ "OR work_packages.due_date < #{today}")
<<~SQL.squish
WITH
@@ -125,4 +124,12 @@ class Notifications::CreateDateAlertsNotificationsJob::AlertableWorkPackages
join_dependency = work_packages.construct_join_dependency([:status], Arel::Nodes::OuterJoin)
work_packages.joins(join_dependency)
end
+
+ def alertable_dates
+ dates = UserPreferences::ParamsContract::DATE_ALERT_DURATIONS
+ .compact
+ .map { |offset| Arel::Nodes::build_quoted(Date.current + offset.days) }
+
+ Arel::Nodes::Grouping.new(dates).to_sql
+ end
end
diff --git a/db/migrate/20230105134940_work_package_date_indices.rb b/db/migrate/20230105134940_work_package_date_indices.rb
new file mode 100644
index 00000000000..9235e61a574
--- /dev/null
+++ b/db/migrate/20230105134940_work_package_date_indices.rb
@@ -0,0 +1,6 @@
+class WorkPackageDateIndices < ActiveRecord::Migration[7.0]
+ def change
+ add_index :work_packages, :start_date
+ add_index :work_packages, :due_date
+ end
+end