Skip to content

Optimize populate_mentions with batch query for N+1 performance #163

Closed as not planned

Description

@malibio

Context

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).

Current Behavior

Location: packages/core/src/services/node_service.rs:962, 1035

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:

  1. Collects all node IDs from the result set
  2. Executes 2 batch queries using WHERE node_id IN (...)
  3. Distributes results back to nodes in-memory

Example implementation sketch:

async fn populate_mentions_batch(&self, nodes: &mut [Node]) -> Result<(), NodeServiceError> {
    if nodes.is_empty() { return Ok(()); }
    
    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
}

Priority

Medium - Optimize when:

  • Queries regularly return >50 nodes
  • Performance monitoring shows slow query times
  • Mentions table has significant growth

References

Activity

  1. added
    enhancement
    on Oct 5, 2025
  2. malibio commented on Jan 26, 2026

    @malibio
    CollaboratorAuthor

    Closing this issue as no longer relevant.

    Analysis

    The N+1 query problem described in this issue no longer exists due to architectural changes:

    1. 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)

    2. 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.

    3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions