Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

perf(server): avoid filter doc snapshot by non-indexed updatedAt (#8160)

forehalo87e9ff01b518
diff --git a/packages/backend/server/src/core/doc/adapters/workspace.ts b/packages/backend/server/src/core/doc/adapters/workspace.ts
index 710e07ac0c..6e4e15bc6f 100644
--- a/packages/backend/server/src/core/doc/adapters/workspace.ts
+++ b/packages/backend/server/src/core/doc/adapters/workspace.ts
@@ -143,6 +143,8 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
}
async getSpaceDocTimestamps(workspaceId: string, after?: number) {
+ // TODO(@forehalo): do we need a [Clock] table to store the last seen time of each doc?
+ // SLOW if query DB in large workspace by `updatedAt: { gt: after }`
const snapshots = await this.db.snapshot.findMany({
select: {
id: true,
@@ -150,13 +152,6 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
},
where: {
workspaceId,
- ...(after
- ? {
- updatedAt: {
- gt: new Date(after),
- },
- }
- : {}),
},
});
@@ -165,6 +160,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
workspaceId,
...(after
? {
+ // [createdAt] in updates table is indexed, so it's fast
createdAt: {
gt: new Date(after),
},
@@ -180,7 +176,9 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
const result: Record<string, number> = {};
snapshots.forEach(s => {
- result[s.id] = s.updatedAt.getTime();
+ if (!after || s.updatedAt.getTime() > after) {
+ result[s.id] = s.updatedAt.getTime();
+ }
});
updates.forEach(u => {