From 97eae4e3add4c31841ddc4858dc62c68ef86b1e6 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 26 Nov 2020 16:29:39 +1100 Subject: [PATCH] PERF: avoid using destroy_all when removing stats (#11358) destroy_all will load all the active record relation and iterate, this can get extremely expensive. --- app/services/destroy_task.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/app/services/destroy_task.rb b/app/services/destroy_task.rb index a366312cb9..57afc0e3d7 100644 --- a/app/services/destroy_task.rb +++ b/app/services/destroy_task.rb @@ -97,17 +97,13 @@ class DestroyTask end def destroy_stats - ApplicationRequest.destroy_all - IncomingLink.destroy_all - UserVisit.destroy_all - UserProfileView.destroy_all - user_profiles = UserProfile.all - user_profiles.each do |user_profile| - user_profile.views = 0 - user_profile.save! - end - PostAction.unscoped.destroy_all - EmailLog.destroy_all + ApplicationRequest.delete_all + IncomingLink.delete_all + UserVisit.delete_all + UserProfileView.delete_all + UserProfile.update_all(views: 0) + PostAction.unscoped.delete_all + EmailLog.delete_all end private