Skip to content

Commit 52b2d92

Browse files
committed
perf(dedupe): 给定向候选配 (type, TRIM(title)) 表达式索引,并用 EXPLAIN 钉住它
定向候选把「该类型前 100 条」换成 `type = ? AND TRIM(title) = ?`,代价随最大类型的行数 线性长。本机 node:sqlite 实测(同类型 1000 / 5000 / 20000 行): 现口径 0.13 / 0.68 / 4.63 ms,加这条索引后 ~0.00 ms。 TRIM 本身不是瓶颈——同句去掉 TRIM 只差 0.07 ms,缺的是一条能把整个条件当 seek 用的 索引;而这是每存一条都要付一次的写路径,20000 行那一档不该赌「用户到不了」。 索引表达式必须与 SQL 逐字相同,写歪一点(用 title、列序不对)优化器静默不认,而行为 用例全绿——所以补一条 EXPLAIN QUERY PLAN 用例,钉的是「这条查询真的选中这条索引」, 不是「索引存在」。存量库靠 CREATE INDEX IF NOT EXISTS 在打开时补上,与 share 那两条 索引同一写法。
1 parent f15190f commit 52b2d92

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎dsh-mneme/lib/store.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,15 @@ export function createStore(path) {
915915
// 进来('' 参与唯一性,第二条 '' 直接撞响),不是防 WHERE 子句消失。
916916
db.exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_memories_share_external ON memories(share_external_id) WHERE share_external_id IS NOT NULL");
917917
db.exec("CREATE INDEX IF NOT EXISTS idx_memories_share_pack ON memories(share_pack_id)");
918+
// Issue #505:saveWithDedupe 的定向候选是 `type = ? AND TRIM(title) = ?`。只有
919+
// idx_memories_type 时这条语句等于「把该类型的行全读一遍、逐行 TRIM」,代价随最大
920+
// 类型的行数线性长(本机 node:sqlite 实测同类型 1000 / 5000 / 20000 行 = 0.13 /
921+
// 0.68 / 4.63 ms,而它是每存一条都要付一次的写路径)。TRIM 本身不是瓶颈——同句去掉
922+
// TRIM 只差 0.07 ms,缺的是能把整个条件当 seek 用的索引;建好之后同一查询在同一批
923+
// 数据上 ~0.00 ms。索引表达式必须与 SQL 里的写法逐字相同(TRIM(title),不是 title),
924+
// 差一点优化器就静默不认,而所有行为用例照样全绿——所以 dedupe-candidate-window
925+
// 那条用例用 EXPLAIN QUERY PLAN 钉住它真的被选中。改 service 的 WHERE 必须同时改这里。
926+
db.exec("CREATE INDEX IF NOT EXISTS idx_memories_type_trimmed_title ON memories(type, TRIM(title))");
918927
// issue #24 块2(权重演化):entity_relations 加 weight + source。存量关系
919928
// 行缺列 → 默认 manual 1.0(最保守:既有语义不因迁移而变弱)。
920929
addColumn("entity_relations", "weight", "ALTER TABLE entity_relations ADD COLUMN weight REAL NOT NULL DEFAULT 1.0");

‎dsh-mneme/src/store.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,15 @@ export function createStore(path) {
915915
// 进来('' 参与唯一性,第二条 '' 直接撞响),不是防 WHERE 子句消失。
916916
db.exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_memories_share_external ON memories(share_external_id) WHERE share_external_id IS NOT NULL");
917917
db.exec("CREATE INDEX IF NOT EXISTS idx_memories_share_pack ON memories(share_pack_id)");
918+
// Issue #505:saveWithDedupe 的定向候选是 `type = ? AND TRIM(title) = ?`。只有
919+
// idx_memories_type 时这条语句等于「把该类型的行全读一遍、逐行 TRIM」,代价随最大
920+
// 类型的行数线性长(本机 node:sqlite 实测同类型 1000 / 5000 / 20000 行 = 0.13 /
921+
// 0.68 / 4.63 ms,而它是每存一条都要付一次的写路径)。TRIM 本身不是瓶颈——同句去掉
922+
// TRIM 只差 0.07 ms,缺的是能把整个条件当 seek 用的索引;建好之后同一查询在同一批
923+
// 数据上 ~0.00 ms。索引表达式必须与 SQL 里的写法逐字相同(TRIM(title),不是 title),
924+
// 差一点优化器就静默不认,而所有行为用例照样全绿——所以 dedupe-candidate-window
925+
// 那条用例用 EXPLAIN QUERY PLAN 钉住它真的被选中。改 service 的 WHERE 必须同时改这里。
926+
db.exec("CREATE INDEX IF NOT EXISTS idx_memories_type_trimmed_title ON memories(type, TRIM(title))");
918927
// issue #24 块2(权重演化):entity_relations 加 weight + source。存量关系
919928
// 行缺列 → 默认 manual 1.0(最保守:既有语义不因迁移而变弱)。
920929
addColumn("entity_relations", "weight", "ALTER TABLE entity_relations ADD COLUMN weight REAL NOT NULL DEFAULT 1.0");

‎dsh-mneme/test/dedupe-candidate-window.test.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
// 但用更小的数字达成同样的窗口溢出,避免测试本身变慢。
1010
import test from "node:test";
1111
import assert from "node:assert/strict";
12+
import { mkdtempSync, rmSync } from "node:fs";
13+
import { tmpdir } from "node:os";
14+
import { join } from "node:path";
15+
import { DatabaseSync } from "node:sqlite";
1216
import { createStore } from "../src/store.js";
1317
import { createService } from "../src/service.js";
1418

@@ -148,3 +152,29 @@ test("#505: 归档行不作为合并目标(口径与 list 默认一致)", ()
148152
assert.equal(second.action, "created", "已归档行不该被当作活跃合并目标");
149153
assert.notEqual(second.memory.id, first.memory.id);
150154
});
155+
156+
157+
// #505 把「取该类型前 100 条」换成「按 type + TRIM(title) 精确取」:窗口没了、静默不
158+
// 合并也没了,但 SQL 形状变了——代价随最大类型的行数线性长(20000 行时 4.6ms 一次,
159+
// 每存一条付一次),所以 store.js 里配了 (type, TRIM(title)) 表达式索引。这条用例不测
160+
// 快慢(机器有关节,数字不可复现),测的是**那条索引真的被这条查询选中**:表达式写歪
161+
// 一点(漏 TRIM、列序不对)优化器就静默退回扫整个类型,而本文件其余用例全绿。
162+
test("#505: 定向候选那条 SQL 命中表达式索引,而不是扫完整个类型", () => {
163+
const dir = mkdtempSync(join(tmpdir(), "mneme-dedupe-idx-"));
164+
const file = join(dir, "m.db");
165+
const store = createStore(file);
166+
store.save({ type: "project", title: " spaced ", content: "x", importance: 3 });
167+
store.close();
168+
const db = new DatabaseSync(file);
169+
try {
170+
// WHERE 与 store.js 的 activeByTypeAndTitle 逐字同形,只把列表换成单列。
171+
const plan = db.prepare(
172+
"EXPLAIN QUERY PLAN SELECT id FROM memories WHERE type = ? AND TRIM(title) = ? AND forgotten = 0 AND archived = 0"
173+
).all("project", "spaced").map((r) => r.detail).join(" ");
174+
assert.match(plan, /idx_memories_type_trimmed_title/,
175+
`定向候选必须走 (type, TRIM(title)) 表达式索引,实得:${plan}`);
176+
} finally {
177+
db.close();
178+
rmSync(dir, { recursive: true, force: true });
179+
}
180+
});

0 commit comments

Comments
 (0)