Skip to content

Build your personal knowledge base with Trilium Notes

perf(sync): index-only outstanding-count query

Elian Doran18db0ad04c62
diff --git a/packages/trilium-core/src/routes/api/sync.spec.ts b/packages/trilium-core/src/routes/api/sync.spec.ts
index 69ca7f7fdb..917ee4cfd2 100644
--- a/packages/trilium-core/src/routes/api/sync.spec.ts
+++ b/packages/trilium-core/src/routes/api/sync.spec.ts
@@ -159,6 +159,44 @@ describe("Sync API (core)", () => {
expect(res.body.lastEntityChangeId).toBe(baseId + 2);
expect(res.body.outstandingPullCount).toBe(0);
});
+
+ it("counts outstanding changes by id only (index-only), independent of instanceId", async () => {
+ // The outstanding count is an index-only range count on (isSynced, id) and deliberately does
+ // NOT filter by instanceId. So a change owned by the querying client, sitting beyond the
+ // returned cursor, is now included in the estimate (it is skipped when actually returned, so
+ // the count converges to 0 on the next pull).
+ const sql = getSql();
+ const maxId = sql.getValue<number>("SELECT COALESCE(MAX(id), 0) FROM entity_changes");
+
+ // a real option so the "other instance" change referencing it is returnable
+ sql.execute("INSERT INTO options (name, value, isSynced, utcDateModified) VALUES ('otst_opt', 'v', 1, '2020-01-01T00:00:00.000Z')");
+ // change A (another instance) → returned, advancing the cursor to maxId + 1
+ sql.execute(
+ `INSERT INTO entity_changes (id, entityName, entityId, hash, isErased, changeId, componentId, instanceId, isSynced, utcDateChanged)
+ VALUES (?, 'options', 'otst_opt', 'h', 0, 'otst_A', 'cmp', 'OTHER', 1, '2020-01-01T00:00:00.000Z')`,
+ [maxId + 1]
+ );
+ // change B: the querying client's OWN change, id beyond A → excluded from the returned records
+ // but counted by the index-only outstanding count
+ sql.execute(
+ `INSERT INTO entity_changes (id, entityName, entityId, hash, isErased, changeId, componentId, instanceId, isSynced, utcDateChanged)
+ VALUES (?, 'options', 'otst_optB', 'h', 0, 'otst_B', 'cmp', 'CLIENTX', 1, '2020-01-01T00:00:00.000Z')`,
+ [maxId + 2]
+ );
+
+ const res = await api.get<{ entityChanges: unknown[]; lastEntityChangeId: number; outstandingPullCount: number }>(
+ "/api/sync/changed",
+ { query: { instanceId: "CLIENTX", lastEntityChangeId: String(maxId) } }
+ );
+
+ expect(res.status).toBe(200);
+ expect(res.body.lastEntityChangeId).toBe(maxId + 1); // A returned, cursor advanced past it
+ expect(res.body.outstandingPullCount).toBe(1); // B counted by id, despite being the client's own
+
+ // keep the shared DB clean for other specs
+ sql.execute("DELETE FROM entity_changes WHERE id IN (?, ?)", [maxId + 1, maxId + 2]);
+ sql.execute("DELETE FROM options WHERE name = 'otst_opt'");
+ });
});
describe("update (PUT /api/sync/update)", () => {
diff --git a/packages/trilium-core/src/routes/api/sync.ts b/packages/trilium-core/src/routes/api/sync.ts
index 9250b251c4..ccae8c6ac9 100644
--- a/packages/trilium-core/src/routes/api/sync.ts
+++ b/packages/trilium-core/src/routes/api/sync.ts
@@ -182,18 +182,28 @@ function getChanged(req: Request) {
getLog().info(`Returning ${entityChangeRecords.length} entity changes in ${Date.now() - startTime}ms`);
}
- return {
- entityChanges: entityChangeRecords,
- lastEntityChangeId,
- outstandingPullCount: sql.getValue(
- `
+ // `outstandingPullCount` is a progress estimate returned on every pull request. Counting with
+ // only `isSynced` + `id` is an index-only range scan on IDX_entity_changes_isSynced_id; adding
+ // an `instanceId != ?` filter (instanceId is not in the index) forces a table lookup per row,
+ // which made this query the single most expensive part of serving a large initial sync.
+ //
+ // Dropping the instanceId filter can transiently count the client's own pushed changes (which
+ // are then skipped when returned), so the estimate may over-count slightly mid-sync but always
+ // converges to 0 as `lastEntityChangeId` advances. During an initial sync the client has no rows
+ // on the server, so the count is exact.
+ const outstandingPullCount = sql.getValue(
+ `
SELECT COUNT(id)
FROM entity_changes
WHERE isSynced = 1
- AND instanceId != ?
AND id > ?`,
- [clientInstanceId, lastEntityChangeId]
- )
+ [lastEntityChangeId]
+ );
+
+ return {
+ entityChanges: entityChangeRecords,
+ lastEntityChangeId,
+ outstandingPullCount
};
}