Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
core/content manager/space usage: optimize queries
Adorian Dorane3fcb2fa273c
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.spec.tsxindex 3391aba7c1..58b093d2f7 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.spec.tsx@@ -1,8 +1,6 @@import { render } from "preact";import { afterEach, describe, expect, it, vi } from "vitest";-import type LoadResults from "../../../../../services/load_results";-// Hoisted alongside the mock factories, which can run before this file's top-level constants exist.const mocks = vi.hoisted(() => ({serverGet: undefined as unknown as (url: string) => Promise<unknown>,@@ -16,18 +14,14 @@ vi.mock("../../../../../services/server", () => ({}));// A full mock, not a partial one: the real hooks module transitively imports half the app at module-// scope (app_context, keyboard actions), and the hook under test only needs this one export.-// The subscription is captured so a change can be fired without the app's event plumbing.+// scope (app_context, keyboard actions). Any subscription the hook made would land here, which is+// how the "measures only when asked" test can prove it makes none.vi.mock("../../../../react/hooks", () => ({useTriliumEvent: (_name: string, handler: (data: { loadResults: unknown }) => void) => {mocks.entitiesReloadedHandler = handler;}}));-function fireEntitiesReloaded(loadResults: LoadResults) {- mocks.entitiesReloadedHandler?.({ loadResults });-}-// Import AFTER the mocks (vi.mock is hoisted, but the hook import must resolve the mocked deps).import { useSpaceUsageFetch } from "./use_space_usage_fetch";@@ -44,14 +38,6 @@ function renderProbe(url: string) {return container;}-function loadResultsWith(noteIds: string[]): LoadResults {- return {- getNoteIds: () => noteIds,- getBranchRows: () => [],- getAttachmentRows: () => []- } as unknown as LoadResults;-}-afterEach(() => {if (container) {render(null, container);@@ -112,21 +98,19 @@ describe("useSpaceUsageFetch", () => {expect(probe.textContent).toBe("fast");});- it("refetches once, debounced, when notes change — and not on irrelevant reloads", async () => {+ it("never re-measures on its own when notes change", async () => {serverGet.mockResolvedValue({ label: "data" });const probe = renderProbe("space-usage/overview");await vi.waitFor(() => expect(probe.textContent).toBe("data"));expect(serverGet).toHaveBeenCalledTimes(1);+ // Measuring the database is far too expensive to repeat after every edit; the user asks for+ // a fresh reading instead. A subscription that merely fires later would be just as bad, so+ // assert the hook subscribes to nothing at all.+ expect(mocks.entitiesReloadedHandler).toBeUndefined();+vi.useFakeTimers();- fireEntitiesReloaded(loadResultsWith([]));- await vi.advanceTimersByTimeAsync(1500);+ await vi.advanceTimersByTimeAsync(60_000);expect(serverGet).toHaveBeenCalledTimes(1);-- // A burst of changes coalesces into a single reload.- fireEntitiesReloaded(loadResultsWith([ "changed" ]));- fireEntitiesReloaded(loadResultsWith([ "changed" ]));- await vi.advanceTimersByTimeAsync(1500);- expect(serverGet).toHaveBeenCalledTimes(2);});});diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.tsindex 8557ae57e7..f0541cb1ff 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.ts+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/use_space_usage_fetch.ts@@ -1,11 +1,6 @@-import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";+import { useCallback, useEffect, useRef, useState } from "preact/hooks";-import debounce from "../../../../../services/debounce";import server from "../../../../../services/server";-import { useTriliumEvent } from "../../../../react/hooks";--/** One reload per burst — a subtree delete floods entity changes. */-const REFRESH_DEBOUNCE_MS = 1000;export interface SpaceUsageFetch<T> {data: T | null;@@ -14,11 +9,14 @@ export interface SpaceUsageFetch<T> {}/**- * Fetches a space-usage endpoint and keeps it current: content changes anywhere — deletes, moves,- * uploads — shift the sizes, so any note/branch/attachment change schedules one debounced reload.+ * Fetches a space-usage endpoint once, and again whenever the URL changes (Browse navigation).+ *+ * Deliberately *not* refreshed when notes change: measuring the database is expensive enough that+ * re-running it after every edit would keep the server busy for as long as this page stayed open —+ * and the numbers are a snapshot to read, not a live readout. Re-measuring is the user's call.*- * `null` until the first response; on a URL change (Browse navigation) the previous payload stays- * up while the new one is in flight, so the charts transition instead of blanking.+ * `null` until the first response; on a URL change the previous payload stays up while the new one+ * is in flight, so the charts transition instead of blanking.** A failure is reported rather than swallowed, because there is nothing to fall back on before the* first success: the view would otherwise claim to be measuring for as long as it stayed open. The@@ -52,14 +50,5 @@ export function useSpaceUsageFetch<T>(url: string): SpaceUsageFetch<T> {useEffect(() => { void refresh(); }, [ refresh ]);- const delayedRefresh = useMemo(() => debounce(() => void refresh(), REFRESH_DEBOUNCE_MS), [ refresh ]);- useEffect(() => () => delayedRefresh.clear(), [ delayedRefresh ]);-- useTriliumEvent("entitiesReloaded", ({ loadResults }) => {- if (loadResults.getNoteIds().length || loadResults.getBranchRows().length || loadResults.getAttachmentRows().length) {- delayedRefresh();- }- });-return { data, failed };}diff --git a/packages/trilium-core/src/services/space_usage.ts b/packages/trilium-core/src/services/space_usage.tsindex b9bd49264e..95617c1df1 100644--- a/packages/trilium-core/src/services/space_usage.ts+++ b/packages/trilium-core/src/services/space_usage.ts@@ -35,7 +35,11 @@ export const MAX_OVERVIEW_LIMIT = 1000;const ROOT_NOTE_ID = "root";const HIDDEN_ROOT_ID = "_hidden";-export function getOverview({ includeRevisions, limit }: { includeRevisions: boolean, limit: number }): SpaceUsageOverviewResponse {+export function getOverview(options: { includeRevisions: boolean, limit: number }): SpaceUsageOverviewResponse {+ return withBlobUsage(() => buildOverview(options));+}++function buildOverview({ includeRevisions, limit }: { includeRevisions: boolean, limit: number }): SpaceUsageOverviewResponse {const sizes = collectSizes();const forest = buildForestFromBecca();const rankOf = (entry: SpaceUsageSizes) =>@@ -68,6 +72,13 @@ export function getOverview({ includeRevisions, limit }: { includeRevisions: boo}export function getNoteUsage(noteId: string): SpaceUsageNoteResponse {+ // Resolved before the measuring pass, so a bad note ID costs nothing.+ const note = becca.getNoteOrThrow(noteId);++ return withBlobUsage(() => buildNoteUsage(note.noteId));+}++function buildNoteUsage(noteId: string): SpaceUsageNoteResponse {const note = becca.getNoteOrThrow(noteId);const sizes = collectSizes();const forest = buildForestFromBecca();@@ -87,9 +98,9 @@ export function getNoteUsage(noteId: string): SpaceUsageNoteResponse {const attachmentSizes = getSql().getMap<string, number>(`- SELECT attachments.attachmentId, COALESCE(LENGTH(blobs.content), 0)+ SELECT attachments.attachmentId, blob_usage.sizeFROM attachments- JOIN blobs ON blobs.blobId = attachments.blobId+ JOIN blob_usage ON blob_usage.blobId = attachments.blobIdWHERE attachments.ownerId = ? AND attachments.isDeleted = 0`,[ noteId ]);@@ -282,55 +293,56 @@ export function buildNotePath(parentByNoteId: Map<string, string>, noteId: strinreturn path.reverse();}-interface SizeLookup {- own: Record<string, number>;- attachments: Record<string, number>;- revisions: Record<string, number>;-}+type SizeLookup = Map<string, SpaceUsageSizes>;-/** One grouped pass per component over the blobs — the whole request's byte counting. */+/**+ * Every note's own components, in one grouped query over the measured blobs. Notes holding nothing+ * produce no row and are simply absent, which keeps the map to what actually occupies space.+ */function collectSizes(): SizeLookup {- const sql = getSql();-- const own = sql.getMap<string, number>(`- SELECT notes.noteId, COALESCE(LENGTH(blobs.content), 0)- FROM notes- JOIN blobs ON blobs.blobId = notes.blobId- WHERE notes.isDeleted = 0`);+ const rows = getSql().getRows<{+ noteId: string, ownSize: number, attachmentsSize: number, revisionsSize: number+ }>(`+ SELECT noteId,+ SUM(own) AS ownSize,+ SUM(attachment) AS attachmentsSize,+ SUM(revision) AS revisionsSize+ FROM (+ SELECT notes.noteId AS noteId, blob_usage.size AS own, 0 AS attachment, 0 AS revision+ FROM notes+ JOIN blob_usage ON blob_usage.blobId = notes.blobId+ WHERE notes.isDeleted = 0- // Owners that are revisions rather than notes simply never match a live noteId here; their- // attachments are picked up by the revisions query below.- const attachments = sql.getMap<string, number>(`- SELECT attachments.ownerId, SUM(COALESCE(LENGTH(blobs.content), 0))- FROM attachments- JOIN blobs ON blobs.blobId = attachments.blobId- WHERE attachments.isDeleted = 0- GROUP BY attachments.ownerId`);+ UNION ALL+ -- Owners that are revisions rather than notes never match a live noteId here; those+ -- attachments are picked up by the revision branch below.+ SELECT attachments.ownerId, 0, blob_usage.size, 0+ FROM attachments+ JOIN blob_usage ON blob_usage.blobId = attachments.blobId+ WHERE attachments.isDeleted = 0- const revisions = sql.getMap<string, number>(`- SELECT noteId, SUM(size)- FROM (- SELECT revisions.noteId AS noteId, COALESCE(LENGTH(blobs.content), 0) AS size+ UNION ALL+ SELECT revisions.noteId, 0, 0, blob_usage.sizeFROM revisions- JOIN blobs ON blobs.blobId = revisions.blobId+ JOIN blob_usage ON blob_usage.blobId = revisions.blobId+UNION ALL- SELECT revisions.noteId, COALESCE(LENGTH(blobs.content), 0)+ SELECT revisions.noteId, 0, 0, blob_usage.sizeFROM attachmentsJOIN revisions ON revisions.revisionId = attachments.ownerId- JOIN blobs ON blobs.blobId = attachments.blobId+ JOIN blob_usage ON blob_usage.blobId = attachments.blobIdWHERE attachments.isDeleted = 0)GROUP BY noteId`);- return { own, attachments, revisions };+ return new Map(rows.map(({ noteId, ownSize, attachmentsSize, revisionsSize }) =>+ [ noteId, { ownSize, attachmentsSize, revisionsSize } ]));}+const NO_SIZES: SpaceUsageSizes = { ownSize: 0, attachmentsSize: 0, revisionsSize: 0 };+function sizesOf(noteId: string, sizes: SizeLookup): SpaceUsageSizes {- return {- ownSize: sizes.own[noteId] ?? 0,- attachmentsSize: sizes.attachments[noteId] ?? 0,- revisionsSize: sizes.revisions[noteId] ?? 0- };+ return sizes.get(noteId) ?? NO_SIZES;}function bucketOf(entries: SpaceUsageSizes[]): SpaceUsageBucket {@@ -344,6 +356,24 @@ function bucketOf(entries: SpaceUsageSizes[]): SpaceUsageBucket {return bucket;}+/**+ * What still keeps a blob alive, in ascending priority. A blob claimed by several kinds is+ * attributed to the highest, which is what makes the reported tiers add up to the total instead of+ * counting shared content twice.+ */+const BLOB_UNREFERENCED = 0;+const BLOB_DELETED = 1;+const BLOB_REVISION = 2;+const BLOB_ATTACHMENT = 3;+const BLOB_BODY = 4;++const DELETED_BLOBS = `+ SELECT blobId FROM notes WHERE isDeleted = 1 AND blobId IS NOT NULL+ UNION SELECT blobId FROM attachments WHERE isDeleted = 1 AND blobId IS NOT NULL+ UNION SELECT revisions.blobId FROM revisions+ JOIN notes ON notes.noteId = revisions.noteId+ WHERE notes.isDeleted = 1 AND revisions.blobId IS NOT NULL`;+const LIVE_BODY_BLOBS = `SELECT blobId FROM notes WHERE isDeleted = 0 AND blobId IS NOT NULL`;const LIVE_NOTE_ATTACHMENT_BLOBS = `@@ -361,6 +391,55 @@ const LIVE_REVISION_BLOBS = `JOIN notes ON notes.noteId = revisions.noteIdWHERE attachments.isDeleted = 0 AND notes.isDeleted = 0 AND attachments.blobId IS NOT NULL`;+/**+ * Measures every blob exactly once into a temporary table, tags each with what still keeps it+ * alive, and runs the caller against that.+ *+ * This is the only pass over blob content a request makes, and it is what keeps these endpoints+ * usable on a large database. Measuring is the expensive part — everything downstream then joins+ * integers. The shape this replaced re-measured the whole database for each figure it reported,+ * seven times over; since the SQLite connection is synchronous, the server could serve nothing at+ * all meanwhile, so a slow report froze the whole app rather than just this page.+ */+function withBlobUsage<T>(collect: () => T): T {+ const sql = getSql();++ // A leftover from a request that died mid-flight would otherwise be reused as if it were fresh.+ sql.execute("DROP TABLE IF EXISTS blob_usage");++ try {+ sql.execute(`+ CREATE TEMP TABLE blob_usage (+ blobId TEXT NOT NULL PRIMARY KEY,+ size INTEGER NOT NULL,+ kind INTEGER NOT NULL DEFAULT ${BLOB_UNREFERENCED}+ )`);++ // `octet_length`, not `length`: on a text value `length` counts *characters*, so it has to+ // decode every note in the database. Bytes are what a space report is about anyway — the+ // character count also under-reported anything non-ASCII.+ sql.execute(`+ INSERT INTO blob_usage (blobId, size)+ SELECT blobId, COALESCE(octet_length(content), 0) FROM blobs`);++ // Claims in ascending priority, each overwriting the last, so a blob ends up under the+ // strongest thing holding it. These walk the reference tables only — no content is read.+ for (const [ kind, referencedBy ] of [+ [ BLOB_DELETED, DELETED_BLOBS ],+ [ BLOB_REVISION, LIVE_REVISION_BLOBS ],+ [ BLOB_ATTACHMENT, LIVE_NOTE_ATTACHMENT_BLOBS ],+ [ BLOB_BODY, LIVE_BODY_BLOBS ]+ ] as const) {+ sql.execute(`UPDATE blob_usage SET kind = ${kind} WHERE blobId IN (${referencedBy})`);+ }++ return collect();+ } finally {+ // The connection outlives the request, so the table must not linger for the next one.+ sql.execute("DROP TABLE IF EXISTS blob_usage");+ }+}+/*** What the live content actually occupies on disk, deduplicated, broken into mutually exclusive* tiers: bodies claim their blobs first, then note-owned attachments, then revisions (snapshot@@ -371,23 +450,16 @@ const LIVE_REVISION_BLOBS = `*/function collectContent(): SpaceUsageContent {const sql = getSql();- const sumBlobs = (tier: string) =>- sql.getValue<number | null>(`- SELECT SUM(COALESCE(LENGTH(blobs.content), 0))- FROM blobs- WHERE ${tier}`) ?? 0;-- const bodiesSize = sumBlobs(`blobs.blobId IN (${LIVE_BODY_BLOBS})`);- const attachmentsSize = sumBlobs(`- blobs.blobId IN (${LIVE_NOTE_ATTACHMENT_BLOBS})- AND blobs.blobId NOT IN (${LIVE_BODY_BLOBS})`);- const revisionsSize = sumBlobs(`- blobs.blobId IN (${LIVE_REVISION_BLOBS})- AND blobs.blobId NOT IN (${LIVE_BODY_BLOBS})- AND blobs.blobId NOT IN (${LIVE_NOTE_ATTACHMENT_BLOBS})`);+ const byKind = new Map(sql+ .getRows<{ kind: number, size: number }>(`SELECT kind, SUM(size) AS size FROM blob_usage GROUP BY kind`)+ .map(({ kind, size }) => [ kind, size ]));+ const sizeOf = (kind: number) => byKind.get(kind) ?? 0;++ const attachmentsSize = sizeOf(BLOB_ATTACHMENT);+ const revisionsSize = sizeOf(BLOB_REVISION);return {- size: bodiesSize + attachmentsSize + revisionsSize,+ size: sizeOf(BLOB_BODY) + attachmentsSize + revisionsSize,noteCount: sql.getValue<number>(`SELECT COUNT(*) FROM notes WHERE isDeleted = 0`),attachmentsSize,revisionsSize@@ -404,9 +476,9 @@ function collectContentSizeOf(noteIds: string[]): number {sql.fillParamList(noteIds);return sql.getValue<number | null>(`- SELECT SUM(COALESCE(LENGTH(blobs.content), 0))- FROM blobs- WHERE blobs.blobId IN (+ SELECT SUM(blob_usage.size)+ FROM blob_usage+ WHERE blob_usage.blobId IN (SELECT notes.blobId FROM notesJOIN param_list ON param_list.paramId = notes.noteIdWHERE notes.isDeleted = 0 AND notes.blobId IS NOT NULL@@ -436,9 +508,9 @@ function collectRevisionsSizeOf(noteIds: string[]): number {sql.fillParamList(noteIds);return sql.getValue<number | null>(`- SELECT SUM(COALESCE(LENGTH(blobs.content), 0))- FROM blobs- WHERE blobs.blobId IN (+ SELECT SUM(blob_usage.size)+ FROM blob_usage+ WHERE blob_usage.blobId IN (SELECT revisions.blobId FROM revisionsJOIN param_list ON param_list.paramId = revisions.noteIdWHERE revisions.blobId IS NOT NULL@@ -447,8 +519,9 @@ function collectRevisionsSizeOf(noteIds: string[]): number {JOIN param_list ON param_list.paramId = revisions.noteIdWHERE attachments.isDeleted = 0 AND attachments.blobId IS NOT NULL)- AND blobs.blobId NOT IN (${LIVE_BODY_BLOBS})- AND blobs.blobId NOT IN (${LIVE_NOTE_ATTACHMENT_BLOBS})`) ?? 0;+ -- The same subtraction the database-wide tiers make, read off the tag each blob already+ -- carries: one still held by a live body or note attachment frees nothing.+ AND blob_usage.kind NOT IN (${BLOB_BODY}, ${BLOB_ATTACHMENT})`) ?? 0;}/**@@ -461,30 +534,17 @@ function collectRevisionsSizeOf(noteIds: string[]): number {function collectDeletedNotes(): SpaceUsageDeletedNotes {const sql = getSql();- const size = sql.getValue<number | null>(`- SELECT SUM(COALESCE(LENGTH(blobs.content), 0))- FROM blobs- WHERE blobs.blobId IN (- SELECT blobId FROM notes WHERE isDeleted = 1- UNION SELECT blobId FROM attachments WHERE isDeleted = 1- UNION SELECT revisions.blobId FROM revisions- JOIN notes ON notes.noteId = revisions.noteId- WHERE notes.isDeleted = 1- ) AND blobs.blobId NOT IN (- SELECT blobId FROM notes WHERE isDeleted = 0 AND blobId IS NOT NULL- UNION SELECT blobId FROM attachments WHERE isDeleted = 0 AND blobId IS NOT NULL- UNION SELECT revisions.blobId FROM revisions- JOIN notes ON notes.noteId = revisions.noteId- WHERE notes.isDeleted = 0 AND revisions.blobId IS NOT NULL- )`) ?? 0;-- const noteCount = sql.getValue<number>(`- SELECT COUNT(*)- FROM notes- JOIN blobs ON blobs.blobId = notes.blobId- WHERE notes.isDeleted = 1`);-- return { size, noteCount };+ return {+ // Blobs no live entity claimed: the tagging already resolved that, a live claim of any kind+ // having overwritten the deleted one.+ size: sql.getValue<number | null>(+ `SELECT SUM(size) FROM blob_usage WHERE kind = ${BLOB_DELETED}`) ?? 0,+ noteCount: sql.getValue<number>(`+ SELECT COUNT(*)+ FROM notes+ JOIN blob_usage ON blob_usage.blobId = notes.blobId+ WHERE notes.isDeleted = 1`)+ };}function buildForestFromBecca(): CanonicalForest {