Skip to content

Index room_agents on agent_id (CHOO-3667) - #1260

Closed
abeldantas wants to merge 3 commits into
mainfrom
room-agents-index
Closed

abeldantas wants to merge 3 commits into
mainfrom
room-agents-index

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

Seven RoomStore reads by agent, and the check of fk_room_agents_agent when an agent is deleted, filter room_agents by agent_id. The primary key is (room_id, agent_id), so each was a sequential scan. In this PR we index agent_id.

room_agents rows without the index with it
56,609, Aurora, 10,000 agents 2.9 ms not measured yet
56,609, local Postgres 16 0.88 ms 0.026 ms

Every process reads the rooms of a controller's agents this way when the controller connects, one agent at a time. Expand only, and not CONCURRENTLY, as for ix_client_rooms_room_id.

Reading the rooms an agent is in (RoomStore.get_rooms_for_agent and six
other reads by agent), and the foreign key's check when an agent is
deleted, filter room_agents by agent. Its primary key leads with the
room, so each was a sequential scan, 2.9 ms at 10,000 agents, and every
process runs it for each agent of a controller that connects. Expand
only, and not CONCURRENTLY, as for ix_client_rooms_room_id.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the index migration, metadata registration, or changelog update.

No open issues 🎉

What changed in this PR

Adds an index on room_agents.agent_id to improve agent-based room lookups and foreign-key checks during agent deletion, without changing application behavior.

Changes:

  • Adds an expand-only Alembic migration for ix_room_agents_agent_id.
  • Registers the index in the SQLAlchemy metadata.
  • Documents the performance improvement in the changelog.
File Description
core/​switch_core/​migrations/​versions/​063652bcb450_index_room_agents_agent_id.py Creates and removes the agent_id index through the migration chain.
core/​switch_core/​db/​models.py Declares the new room_agents.agent_id index in the schema metadata.
CHANGELOG.md Records the improved performance of agent room lookups.

Review effort: Lite

@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants