perf(conversations): optimize /conversations/meta with counter cache for all_count

This commit is contained in:
Tanmay Deep Sharma
2026-02-03 14:20:35 +05:30
parent c884cdefde
commit 6a053e975e
5 changed files with 20 additions and 3 deletions
+1 -1
View File
@@ -170,7 +170,7 @@ class ConversationFinder
[
@conversations.assigned_to(current_user).count,
@conversations.unassigned.count,
@conversations.count
current_account.conversations_count
]
end
+1 -1
View File
@@ -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
@@ -0,0 +1,5 @@
class AddConversationsCountToAccounts < ActiveRecord::Migration[7.1]
def change
add_column :accounts, :conversations_count, :integer, default: 0, null: false
end
end
@@ -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
+2 -1
View File
@@ -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