From 6a053e975e7a74e6cf7448351c5dd71af6614675 Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Tue, 3 Feb 2026 14:20:35 +0530 Subject: [PATCH] perf(conversations): optimize /conversations/meta with counter cache for all_count --- app/finders/conversation_finder.rb | 2 +- app/models/conversation.rb | 2 +- ...60203062506_add_conversations_count_to_accounts.rb | 5 +++++ .../20260203062518_backfill_conversations_count.rb | 11 +++++++++++ db/schema.rb | 3 ++- 5 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20260203062506_add_conversations_count_to_accounts.rb create mode 100644 db/migrate/20260203062518_backfill_conversations_count.rb diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index d43ed31e7..458881367 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -170,7 +170,7 @@ class ConversationFinder [ @conversations.assigned_to(current_user).count, @conversations.unassigned.count, - @conversations.count + current_account.conversations_count ] end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index ca53238e8..583c046a1 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -97,7 +97,7 @@ class Conversation < ApplicationRecord ).sort_on_last_user_message_at } - belongs_to :account + belongs_to :account, counter_cache: true belongs_to :inbox belongs_to :assignee, class_name: 'User', optional: true, inverse_of: :assigned_conversations belongs_to :assignee_agent_bot, class_name: 'AgentBot', optional: true diff --git a/db/migrate/20260203062506_add_conversations_count_to_accounts.rb b/db/migrate/20260203062506_add_conversations_count_to_accounts.rb new file mode 100644 index 000000000..52571e577 --- /dev/null +++ b/db/migrate/20260203062506_add_conversations_count_to_accounts.rb @@ -0,0 +1,5 @@ +class AddConversationsCountToAccounts < ActiveRecord::Migration[7.1] + def change + add_column :accounts, :conversations_count, :integer, default: 0, null: false + end +end diff --git a/db/migrate/20260203062518_backfill_conversations_count.rb b/db/migrate/20260203062518_backfill_conversations_count.rb new file mode 100644 index 000000000..27b112593 --- /dev/null +++ b/db/migrate/20260203062518_backfill_conversations_count.rb @@ -0,0 +1,11 @@ +class BackfillConversationsCount < ActiveRecord::Migration[7.1] + def up + Account.find_each do |account| + Account.reset_counters(account.id, :conversations) + end + end + + def down + # No-op + end +end diff --git a/db/schema.rb b/db/schema.rb index fd4d18cb1..aede09f12 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2026_01_30_061021) do +ActiveRecord::Schema[7.1].define(version: 2026_02_03_062518) do # These extensions should be enabled to support this database enable_extension "pg_stat_statements" enable_extension "pg_trgm" @@ -73,6 +73,7 @@ ActiveRecord::Schema[7.1].define(version: 2026_01_30_061021) do t.integer "status", default: 0 t.jsonb "internal_attributes", default: {}, null: false t.jsonb "settings", default: {} + t.integer "conversations_count", default: 0, null: false t.index ["status"], name: "index_accounts_on_status" end