perf(me): optimize mentions query with materialized CTEs scoped to candidates
The `query_mentioned_in` SQL previously joined notes directly against the full issues/merge_requests tables, with per-row subqueries for author/assignee/reviewer exclusion. On large databases this produced pathological query plans where SQLite scanned the entire notes table before filtering to relevant entities. Refactor into a dedicated `build_mentioned_in_sql()` builder that: 1. Pre-filters candidate issues and MRs into MATERIALIZED CTEs (state open OR recently closed, not authored by user, not assigned/reviewing). This narrows the working set before any notes join. 2. Computes note timestamps (my_ts, others_ts, any_ts) as separate MATERIALIZED CTEs scoped to candidate entities only, rather than scanning all notes. 3. Joins mention-bearing notes against the pre-filtered candidates, avoiding the full-table scans. Also adds a test verifying that authored issues are excluded from the mentions results, and a unit test asserting all four CTEs are materialized. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -880,6 +880,21 @@ fn mentioned_in_excludes_assigned_issue() {
|
||||
assert!(results.is_empty(), "should exclude assigned issues");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mentioned_in_excludes_authored_issue() {
|
||||
let conn = setup_test_db();
|
||||
insert_project(&conn, 1, "group/repo");
|
||||
insert_issue(&conn, 10, 1, 42, "alice"); // alice IS author
|
||||
let disc_id = 100;
|
||||
insert_discussion(&conn, disc_id, 1, None, Some(10));
|
||||
let t = now_ms() - 1000;
|
||||
insert_note_at(&conn, 200, disc_id, 1, "bob", false, "hey @alice", t);
|
||||
|
||||
let recency_cutoff = now_ms() - 7 * 24 * 3600 * 1000;
|
||||
let results = query_mentioned_in(&conn, "alice", &[], recency_cutoff).unwrap();
|
||||
assert!(results.is_empty(), "should exclude authored issues");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mentioned_in_finds_mention_on_non_authored_mr() {
|
||||
let conn = setup_test_db();
|
||||
@@ -1093,6 +1108,27 @@ fn mentioned_in_rejects_false_positive_email() {
|
||||
|
||||
// ─── Helper Tests ──────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn mentioned_in_sql_materializes_core_ctes() {
|
||||
let sql = build_mentioned_in_sql("");
|
||||
assert!(
|
||||
sql.contains("candidate_issues AS MATERIALIZED"),
|
||||
"candidate_issues should be materialized"
|
||||
);
|
||||
assert!(
|
||||
sql.contains("candidate_mrs AS MATERIALIZED"),
|
||||
"candidate_mrs should be materialized"
|
||||
);
|
||||
assert!(
|
||||
sql.contains("note_ts_issue AS MATERIALIZED"),
|
||||
"note_ts_issue should be materialized"
|
||||
);
|
||||
assert!(
|
||||
sql.contains("note_ts_mr AS MATERIALIZED"),
|
||||
"note_ts_mr should be materialized"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_attention_state_all_variants() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user