Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changes/perf-purge-vector-hashes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Fixed

- **A conversation rebuild no longer spends minutes dropping the old build's vectors.** For each old chunk text the
check for a current chunk with the same text read every conversation chunk in the store, and the delete that follows
read the whole vector key; both now use their index. A rebuild of a forum of about 112,000 messages took 889 s and now
takes 52 s, on the same store and load.
14 changes: 10 additions & 4 deletions src/store/sqlite/vectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ export const chunkFreshness = (
*/
export const purgeVectorHashes = (context: StoreContext, hashes: readonly string[]): void => {
const { orm } = context
for (const hash of new Set(hashes)) {
const unique = new Set(hashes)
if (unique.size === 0) return
const models = orm.all<{ model: string }>(sql`SELECT DISTINCT model FROM embeddings`).map(({ model }) => model)
for (const hash of unique) {
let offset = 0
let valid = false
for (;;) {
// `+` keeps SQLite on the hash index: by chunk type it read every conversation chunk for each hash.
const candidates = orm.all<{
conversation: number
first: number
Expand All @@ -106,7 +110,7 @@ export const purgeVectorHashes = (context: StoreContext, hashes: readonly string
}>(sql`SELECT k.chunkable_id AS conversation, r.first_message_id AS first,
r.last_message_id AS last, r.text_start AS start, r.text_end AS end
FROM chunks k JOIN chunk_messages r ON r.chunk_id = k.id
JOIN conversations c ON k.chunkable_type = 'conversation' AND c.id = k.chunkable_id
JOIN conversations c ON +k.chunkable_type = 'conversation' AND c.id = k.chunkable_id
JOIN conversation_state s ON s.chat_id = c.chat_id AND s.current_build = c.build
WHERE k.content_hash = ${hash} ORDER BY k.chunkable_id, k.position LIMIT 100 OFFSET ${offset}`)
valid = candidates.some(({ conversation, first, last, start, end }) => {
Expand All @@ -120,9 +124,11 @@ export const purgeVectorHashes = (context: StoreContext, hashes: readonly string
break
}
}
// By the full key, model first: by hash alone each delete read the whole key.
if (!valid)
orm.run(sql`DELETE FROM embeddings WHERE content_hash = ${hash}
AND NOT EXISTS (SELECT 1 FROM chunks WHERE content_hash = ${hash} AND chunkable_type <> 'conversation')`)
for (const model of models)
orm.run(sql`DELETE FROM embeddings WHERE model = ${model} AND content_hash = ${hash}
AND NOT EXISTS (SELECT 1 FROM chunks WHERE content_hash = ${hash} AND chunkable_type <> 'conversation')`)
}
}

Expand Down
Loading