Skip to content

fix: eliminate N+1 query in _load_candidate_sessions for event_type filter#134

Closed
acailic wants to merge 1 commit into
mainfrom
fix/132-n-plus-1-event-type-filter
Closed

fix: eliminate N+1 query in _load_candidate_sessions for event_type filter#134
acailic wants to merge 1 commit into
mainfrom
fix/132-n-plus-1-event-type-filter

Conversation

@acailic
Copy link
Copy Markdown
Owner

@acailic acailic commented Apr 3, 2026

Summary

Changes

  • storage/search.py — move event_type filtering into the main SQL statement via SessionModel.id.in_(subquery) before executing; remove the post-fetch loop
  • tests/test_nl_search_api.py — add test_nl_search_event_type_filter_uses_single_query to verify sessions without the requested event type are excluded and sessions with it are returned

Test plan

  • ruff check . — passes
  • python3 -m pytest -q tests/test_nl_search_api.py -k event_type — 1 passed
  • python3 -m pytest -q — 2108 passed, 59 skipped, 3 pre-existing failures in test_analytics_db.py (date-mocking issue unrelated to this change, reproduced on main)

🤖 Generated with Claude Code

…ilter

Replace per-session SELECT loop with a single IN subquery, keeping total
query count at 1 regardless of candidate session count. Fixes #132.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 3, 2026 22:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Eliminates an N+1 query pattern in SessionSearchService._load_candidate_sessions when applying an event_type filter by pushing the filtering into SQL via a subquery, and adds a regression test around event_type filtering behavior.

Changes:

  • Refactors _load_candidate_sessions to apply event_type filtering in the SQL statement (removing the per-session lookup loop).
  • Adds a new NL search API test to ensure sessions are included/excluded appropriately when event_type is provided.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
storage/search.py Pushes event_type filtering into SQL to remove per-session queries.
tests/test_nl_search_api.py Adds a regression test for event_type filter behavior in NL search results.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +387 to +391
async def test_nl_search_event_type_filter_uses_single_query(db_session):
"""event_type filter must resolve in a single SQL query, not N+1 per session.

Regression test for: N+1 query in _load_candidate_sessions when filtering
by event_type (issue #132). The fix pushes filtering into SQL via IN subquery.
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

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

This test is named/documented as enforcing a single SQL query, but it never asserts query count. Also, because it calls search_sessions_nl, the search path still issues per-session event-loading queries during scoring/highlights, so a true “single query” assertion would be unreliable here. Consider either (a) renaming/updating the docstring to only assert correct filtering behavior, or (b) moving the regression to a unit-level test that targets _load_candidate_sessions directly and counts execute/cursor calls for that method only.

Suggested change
async def test_nl_search_event_type_filter_uses_single_query(db_session):
"""event_type filter must resolve in a single SQL query, not N+1 per session.
Regression test for: N+1 query in _load_candidate_sessions when filtering
by event_type (issue #132). The fix pushes filtering into SQL via IN subquery.
async def test_nl_search_event_type_filter_returns_only_matching_sessions(db_session):
"""Regression test for correct event_type filtering in NL search results.
This test verifies that sessions with a matching event_type are returned and
sessions without that event_type are excluded when searching via
search_sessions_nl. It does not assert SQL query counts.

Copilot uses AI. Check for mistakes.
@acailic acailic closed this Apr 3, 2026
@acailic acailic deleted the fix/132-n-plus-1-event-type-filter branch April 3, 2026 23:59
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.

bug: N+1 query in _load_candidate_sessions when filtering by event_type

2 participants