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

This commit is contained in:
forehalo
2024-09-08 12:03:55 +00:00
parent 32d3769201
commit 87e9ff01b5

View File

@@ -143,6 +143,8 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
} }
async getSpaceDocTimestamps(workspaceId: string, after?: number) { 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({ const snapshots = await this.db.snapshot.findMany({
select: { select: {
id: true, id: true,
@@ -150,13 +152,6 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
}, },
where: { where: {
workspaceId, workspaceId,
...(after
? {
updatedAt: {
gt: new Date(after),
},
}
: {}),
}, },
}); });
@@ -165,6 +160,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
workspaceId, workspaceId,
...(after ...(after
? { ? {
// [createdAt] in updates table is indexed, so it's fast
createdAt: { createdAt: {
gt: new Date(after), gt: new Date(after),
}, },
@@ -180,7 +176,9 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
const result: Record<string, number> = {}; const result: Record<string, number> = {};
snapshots.forEach(s => { snapshots.forEach(s => {
result[s.id] = s.updatedAt.getTime(); if (!after || s.updatedAt.getTime() > after) {
result[s.id] = s.updatedAt.getTime();
}
}); });
updates.forEach(u => { updates.forEach(u => {