Repository navigation
Description
Problem
GET /api/groups currently eager-loads every User row attached to every returned group only to compute total_users with len(group.users). On installations with many users this makes response time and memory use grow with the number and width of user records, even though the response only needs a count.
The association-table primary keys are ordered (user_id, groups_id) and (inbound_id, group_id). Group-centric counts, joins, and bulk deletes therefore lack a deterministic reverse access path on databases that do not automatically index foreign-key columns.
Evidence
Synthetic local SQLite benchmark with 500,000 user memberships across 100 groups:
| Path | Time | Traced Python allocations | Query plan |
|---|---|---|---|
| Current full user hydration | 1,014.19 ms | 74.43 MiB | Loads user rows |
| SQL count, no reverse index | 2,196.56 ms median | — | Full association scan |
| SQL count with covering reverse index | 18.016 ms median | 0.0098 MiB peak | Covering-index search |
This benchmark is directional rather than a production guarantee, but it reproduces both scaling problems: object hydration and reverse membership lookup.
Proposed change
- Populate
Group.total_userswith a correlated SQL count expression in the group-list query. - Keep the
usersrelationship unloaded for group summaries. - Add covering reverse indexes:
users_groups_association (groups_id, user_id)inbounds_groups_association (group_id, inbound_id)
- Build/drop the PostgreSQL indexes with
CONCURRENTLYoutside the migration transaction so membership writes remain available during upgrades. - Preserve existing create/update/single-group behavior by setting
total_userswhenever those paths intentionally load the relationship.
Acceptance criteria
/api/groupsreturns the sametotal_usersvalues without loadingGroup.users.- Group-centric membership queries use the new reverse indexes.
- Upgrade and downgrade work on SQLite, PostgreSQL, and MySQL/MariaDB.
- PostgreSQL production upgrades do not use a write-blocking normal index build.
- Regression tests cover the index definitions and unloaded relationship behavior.
Activity
SFARL commentedon Oct 1, 2026
The covering reverse index result is compelling: ~18ms and negligible allocations at 500k memberships is already a good solution, so I would ship the SQL count plus index before adding any cache or projection machinery.
If group summaries later become a very high-frequency read over frequent small membership changes, this is also a direct incremental-count shape. I’m building ivmlite, a SQLite extension that can maintain
GROUP BY groups_id, COUNT(*)across association inserts/deletes, with the list query joining the small result. Its current alpha requiresSTRICTbase tables and explicit refresh, so it is not a transparent SQLAlchemy replacement.The benchmark in this issue would be a good decision gate: compare the indexed 18ms baseline against write capture + refresh, varying membership batch size and reads per write. Our existing evaluation shows incremental maintenance can lose when the changed batch is large: current crossover data.
T3ST3ST3R0N commentedon Oct 11, 2026
Fixed on
dev: #967 makes the group list count members without loading every user, and #772 adds the reverse membership indexes. It ships in the next release. Thanks for the detailed report!