perf: Severe N+1 query loop in AssignableAgentsController#index (100+ queries per request)
Summary
When loading the agent list via GET /api/v1/accounts/{account_id}/assignable_agents, the controller fires over 100+ individual SQL queries (N+1 queries) for an account with ~50 agents, resulting in significant latency and database connection pool contention.
Root Cause Analysis
- In
app/controllers/api/v1/accounts/assignable_agents_controller.rb:
def index
agent_ids = @inboxes.map do |inbox|
authorize inbox, :show?
member_ids = inbox.members.pluck(:user_id)
member_ids
end
agent_ids = agent_ids.inject(:&)
agents = Current.account.users.where(id: agent_ids)
@assignable_agents = (agents + Current.account.administrators).uniq
@agent_bots = @include_ai_assignees ? AgentBot.accessible_to(Current.account) : []
end- In
app/views/api/v1/accounts/assignable_agents/index.json.jbuilder:
json.payload do
json.array! @assignable_agents do |agent|
json.partial! 'api/v1/models/agent', formats: [:json], resource: agent
end
end- In
app/views/api/v1/models/_agent.json.jbuilder: The view renders attributes like:
resource.availability_status(callscurrent_account_user)resource.auto_offline(callscurrent_account_user)resource.role(callscurrent_account_user)resource.thumbnail(callsresource.avatar_url->avatar_attachment-> ActiveStorage blob)resource.custom_role_id(callscurrent_account_user&.custom_role_id)
- In
app/models/concerns/user_attribute_helpers.rb:
def current_account_user
# We want to avoid subsequent queries in case where the association is preloaded.
# using where here will trigger n+1 queries.
account_users.find { |ac_usr| ac_usr.account_id == Current.account.id } if Current.account
endNotice that the comment explicitly highlights that account_users must be preloaded. However, in AssignableAgentsController, neither account_users nor avatar_attachment: :blob are preloaded.
Observed Production Impact
In a production account with 47 agents:
- Executing
GET /api/v1/accounts/1/assignable_agents?inbox_ids[]=7&include_ai_assignees=truelogs:
Completed 200 OK in 821ms (Views: 393.8ms | ActiveRecord: 237.7ms (108 queries, 3 cached))Under concurrent dashboard loads, running 108 queries for a single dropdown list serializes behind Puma thread pools and causes response times to spike beyond 10-15 seconds.
Proposed Solution
In app/controllers/api/v1/accounts/assignable_agents_controller.rb:
def index
@include_ai_assignees = params[:include_ai_assignees].present?
agent_ids = @inboxes.map do |inbox|
authorize inbox, :show?
inbox.members.pluck(:user_id)
end
agent_ids = agent_ids.inject(:&)
all_agent_ids = (agent_ids.to_a + Current.account.administrators.pluck(:id)).uniq
@assignable_agents = Current.account.users
.where(id: all_agent_ids)
.includes(:account_users, avatar_attachment: :blob)
@agent_bots = @include_ai_assignees ? AgentBot.accessible_to(Current.account) : []
endWith this change:
- Query count drops from 108 queries to 4 queries (1 for users, 1 for account_users, 1 for active_storage_attachments, 1 for active_storage_blobs).
- ActiveRecord time drops from ~240ms down to ~15ms.
Source: chatwoot/chatwoot