From 0e35bc609d2f644fd35ed20cc94d1fda0fa1c4ad Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 9 Dec 2021 11:13:29 +0100 Subject: [PATCH] notifications: don't preload dossiers on instructeurs This request currently times out almost every night in production. It's because although Instructeurs are loaded in batches (default batch size is 1000), loading all dossiers for 1000 instructeurs is slow. Turns out the code executed after this query to compute notifications doesn't even use these dossiers. Indeed it is faster not to preload them: both the initial query and the total treatment time are shorter. Here's a quick benchmark made locally (but using production data): - Before this commit: Benchmark.measure { pp Instructeur.includes(assign_to: { procedure: :dossiers }).where(assign_tos: { daily_email_notifications_enabled: true }).limit(100).m ap(&:email_notification_data) } Only the initial query : 35s Total time : 97s - Without preloading dossiers: Benchmark.measure { pp Instructeur.includes(assign_to: :procedure).where(assign_tos: { daily_email_notifications_enabled: true }).limit(100).m ap(&:email_notification_data) } Only the initial query : 0.08s (400x faster) Total time : 29s (3,3x faster) Plus it doesn't timeout, of course. --- app/services/notification_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index 0c3881ac0..8158d7e9e 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -2,7 +2,7 @@ class NotificationService class << self def send_instructeur_email_notification Instructeur - .includes(assign_to: { procedure: :dossiers }) + .includes(assign_to: [:procedure]) .where(assign_tos: { daily_email_notifications_enabled: true }) .find_in_batches { |instructeurs| send_batch_of_instructeurs_email_notification(instructeurs) } end