You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optimize populate_mentions with batch query for N+1 performance #163
From PR #150 code review: populate_mentions() is currently called in a loop for query results, causing N+1 query patterns (2 database queries per node).
When query_nodes_simple() returns N nodes, we execute:
N × 2 additional queries (1 for outgoing mentions, 1 for incoming mentions per node)
For 100 nodes = 200 extra database round-trips
Acceptable For Now
Current scale is fine (<50 nodes per query typically), but this won't scale well as mentions usage grows.
Proposed Solution
Implement populate_mentions_batch() that:
Collects all node IDs from the result set
Executes 2 batch queries using WHERE node_id IN (...)
Distributes results back to nodes in-memory
Example implementation sketch:
asyncfnpopulate_mentions_batch(&self,nodes:&mut[Node]) -> Result<(),NodeServiceError>{if nodes.is_empty(){returnOk(());}let node_ids:Vec<_> = nodes.iter().map(|n| n.id.as_str()).collect();// Single query for ALL outgoing mentions// SELECT node_id, mentions_node_id FROM node_mentions WHERE node_id IN (?, ?, ...)// Single query for ALL incoming mentions // SELECT mentions_node_id, node_id FROM node_mentions WHERE mentions_node_id IN (?, ?, ...)// Distribute results to nodes}
The N+1 query problem described in this issue no longer exists due to architectural changes:
Mentions are now denormalized - mentions and mentioned_by are stored directly in the node table and returned in the same query via build_node_from_hub() (surreal_store.rs:984-1000)
Query methods don't call populate_mentions() - Both query_nodes() and query_nodes_simple() in node_service.rs only apply migrations, not mention population. The mentions data is already embedded in nodes from the database.
Batch queries are efficient - get_nodes_by_ids() fetches all nodes in a single query with mentions included
The populate_mentions() method still exists but appears to be legacy code from before the denormalization. It's only called by single-node get_node() fetches and could potentially be cleaned up in a future refactor.
Resolution: Architecture evolved to eliminate the N+1 pattern - no optimization needed.
Activity
Add TODO comments for N+1 query optimization
Clarify DatabaseServiceAdapter is temporary solution (#150)
malibio commentedon Jan 26, 2026
Closing this issue as no longer relevant.
Analysis
The N+1 query problem described in this issue no longer exists due to architectural changes:
Mentions are now denormalized -
mentionsandmentioned_byare stored directly in thenodetable and returned in the same query viabuild_node_from_hub()(surreal_store.rs:984-1000)Query methods don't call
populate_mentions()- Bothquery_nodes()andquery_nodes_simple()in node_service.rs only apply migrations, not mention population. The mentions data is already embedded in nodes from the database.Batch queries are efficient -
get_nodes_by_ids()fetches all nodes in a single query with mentions includedThe
populate_mentions()method still exists but appears to be legacy code from before the denormalization. It's only called by single-nodeget_node()fetches and could potentially be cleaned up in a future refactor.Resolution: Architecture evolved to eliminate the N+1 pattern - no optimization needed.
Clarify DatabaseServiceAdapter is temporary solution (#150)
Clarify DatabaseServiceAdapter is temporary solution (#150)
Clarify DatabaseServiceAdapter is temporary solution (#150)