From 5a77f6218101b85b360d2ec564928d863bc91964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 22 Jun 2015 14:32:45 +0200 Subject: [PATCH] PERF: poor SQL performances when counting notifications --- app/models/notification.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index 884ddaff82d..014a5b4e83e 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -8,10 +8,8 @@ class Notification < ActiveRecord::Base validates_presence_of :notification_type scope :unread, lambda { where(read: false) } - scope :recent, lambda {|n=nil| n ||= 10; order('notifications.created_at desc').limit(n) } - scope :visible , lambda { where('notifications.topic_id IS NULL OR notifications.topic_id IN ( - SELECT id FROM topics - WHERE deleted_at IS NULL)') } + scope :recent, lambda { |n=nil| n ||= 10; order('notifications.created_at desc').limit(n) } + scope :visible , lambda { joins('LEFT JOIN topics ON notifications.topic_id = topics.id AND topics.deleted_at IS NULL') } after_save :refresh_notification_count after_destroy :refresh_notification_count