Skip to content

Commit f864953

Browse files
committed
more fast
1 parent 92af25d commit f864953

1 file changed

Lines changed: 56 additions & 60 deletions

File tree

src/db/postgres.rs

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ fn push_search_ctes<'a>(
480480
f.file_path,
481481
lp.content_hash,
482482
MIN(lp.chunk_index) AS chunk_index,
483+
(array_agg(lp.highlight_pattern ORDER BY lp.chunk_index ASC))[1] AS highlight_pattern,
484+
(array_agg(lp.highlight_case_sensitive ORDER BY lp.chunk_index ASC))[1] AS highlight_case_sensitive,
483485
tf.total_score,
484486
tf.include_historical
485487
FROM limited_plan lp
@@ -570,6 +572,8 @@ fn push_search_ctes<'a>(
570572
rt.file_path,
571573
rt.content_hash,
572574
rt.chunk_index,
575+
rt.highlight_pattern,
576+
rt.highlight_case_sensitive,
573577
rt.total_score,
574578
rt.include_historical,
575579
COALESCE(bm.branches, bf.fallback_branches, ARRAY[]::TEXT[]) AS branches,
@@ -1727,6 +1731,7 @@ ORDER BY idx
17271731
phase1_qb.push(
17281732
"
17291733
SELECT
1734+
fr.file_id,
17301735
fr.repository,
17311736
fr.commit_sha,
17321737
fr.file_path,
@@ -1737,7 +1742,9 @@ ORDER BY idx
17371742
fr.branches,
17381743
fr.live_branches,
17391744
fr.is_historical,
1740-
fr.snapshot_indexed_at
1745+
fr.snapshot_indexed_at,
1746+
fr.highlight_pattern,
1747+
fr.highlight_case_sensitive
17411748
FROM filtered_ranked fr
17421749
ORDER BY
17431750
fr.total_score DESC,
@@ -1801,47 +1808,47 @@ ORDER BY idx
18011808
let results = if start >= total {
18021809
Vec::new()
18031810
} else {
1804-
let offset = i64::try_from(start).unwrap_or(i64::MAX);
1805-
let limit = i64::try_from(page_size).unwrap_or(i64::MAX);
1806-
1807-
let mut phase2_qb = QueryBuilder::new("");
1808-
push_search_ctes(
1809-
&mut phase2_qb,
1810-
request,
1811-
plan_row_limit,
1812-
fetch_limit,
1813-
file_limit,
1814-
needs_live_branch_filter,
1815-
&symbol_terms,
1816-
);
1817-
phase2_qb.push(
1811+
let end = start.saturating_add(page_size).min(total);
1812+
let page_rows = &ranked_rows[start..end];
1813+
1814+
let mut phase2_qb = QueryBuilder::new(
1815+
"
1816+
WITH paged_files (
1817+
ord,
1818+
file_id,
1819+
repository,
1820+
commit_sha,
1821+
file_path,
1822+
content_hash,
1823+
chunk_index,
1824+
total_score,
1825+
include_historical,
1826+
branches,
1827+
live_branches,
1828+
is_historical,
1829+
snapshot_indexed_at,
1830+
highlight_pattern,
1831+
highlight_case_sensitive
1832+
) AS (
18181833
",
1819-
paged_files AS (
1820-
SELECT
1821-
fr.file_id,
1822-
fr.repository,
1823-
fr.commit_sha,
1824-
fr.file_path,
1825-
fr.content_hash,
1826-
fr.chunk_index,
1827-
fr.total_score,
1828-
fr.include_historical,
1829-
fr.branches,
1830-
fr.live_branches,
1831-
fr.is_historical,
1832-
fr.snapshot_indexed_at
1833-
FROM filtered_ranked fr
1834-
ORDER BY
1835-
fr.total_score DESC,
1836-
fr.repository,
1837-
fr.commit_sha,
1838-
fr.file_path,
1839-
fr.chunk_index
1840-
LIMIT ",
18411834
);
1842-
phase2_qb.push_bind(limit);
1843-
phase2_qb.push(" OFFSET ");
1844-
phase2_qb.push_bind(offset);
1835+
phase2_qb.push_values(page_rows.iter().enumerate(), |mut b, (ord, row)| {
1836+
b.push_bind(ord as i64)
1837+
.push_bind(row.file_id)
1838+
.push_bind(&row.repository)
1839+
.push_bind(&row.commit_sha)
1840+
.push_bind(&row.file_path)
1841+
.push_bind(&row.content_hash)
1842+
.push_bind(row.chunk_index)
1843+
.push_bind(row.total_score)
1844+
.push_bind(row.include_historical)
1845+
.push_bind(&row.branches)
1846+
.push_bind(&row.live_branches)
1847+
.push_bind(row.is_historical)
1848+
.push_bind(row.snapshot_indexed_at)
1849+
.push_bind(&row.highlight_pattern)
1850+
.push_bind(row.highlight_case_sensitive);
1851+
});
18451852
phase2_qb.push(
18461853
"
18471854
)
@@ -1864,16 +1871,11 @@ ORDER BY idx
18641871
AND cbc.chunk_index = pf.chunk_index
18651872
JOIN chunks c
18661873
ON c.chunk_hash = cbc.chunk_hash
1867-
JOIN limited_plan lp
1868-
ON lp.file_id = pf.file_id
1869-
AND lp.content_hash = pf.content_hash
1870-
AND lp.chunk_index = pf.chunk_index
1871-
AND lp.include_historical = pf.include_historical
18721874
CROSS JOIN LATERAL extract_context_with_highlight(
18731875
c.text_content,
1874-
lp.highlight_pattern,
1876+
pf.highlight_pattern,
18751877
3,
1876-
lp.highlight_case_sensitive
1878+
pf.highlight_case_sensitive
18771879
) ctx
18781880
LEFT JOIN LATERAL (
18791881
SELECT
@@ -1883,11 +1885,7 @@ ORDER BY idx
18831885
AND cbc.chunk_index < pf.chunk_index
18841886
) sl ON TRUE
18851887
ORDER BY
1886-
pf.total_score DESC,
1887-
pf.repository,
1888-
pf.commit_sha,
1889-
pf.file_path,
1890-
pf.chunk_index,
1888+
pf.ord,
18911889
ctx.match_line_number",
18921890
);
18931891

@@ -2756,25 +2754,23 @@ struct SearchResultRow {
27562754

27572755
#[derive(sqlx::FromRow, Debug, Clone)]
27582756
struct RankedFileRow {
2759-
repository: String,
27602757
#[allow(dead_code)]
2758+
file_id: i32,
2759+
repository: String,
27612760
commit_sha: String,
27622761
file_path: String,
2763-
#[allow(dead_code)]
27642762
content_hash: String,
2765-
#[allow(dead_code)]
27662763
chunk_index: i32,
2767-
#[allow(dead_code)]
27682764
total_score: f64,
2769-
#[allow(dead_code)]
27702765
include_historical: bool,
27712766
branches: Vec<String>,
2772-
#[allow(dead_code)]
27732767
live_branches: Vec<String>,
2774-
#[allow(dead_code)]
27752768
is_historical: bool,
2776-
#[allow(dead_code)]
27772769
snapshot_indexed_at: Option<DateTime<Utc>>,
2770+
#[allow(dead_code)]
2771+
highlight_pattern: String,
2772+
#[allow(dead_code)]
2773+
highlight_case_sensitive: bool,
27782774
}
27792775

27802776
#[derive(sqlx::FromRow)]

0 commit comments

Comments
 (0)