feat: add index for snapshots (#8163)

This commit is contained in:
darkskygit
2024-09-08 13:49:40 +00:00
parent 57083905ff
commit 2a135d8a93
5 changed files with 31 additions and 15 deletions

View File

@@ -143,8 +143,6 @@ 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,
@@ -152,6 +150,13 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
},
where: {
workspaceId,
...(after
? {
updatedAt: {
gt: new Date(after),
},
}
: {}),
},
});
@@ -176,9 +181,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
const result: Record<string, number> = {};
snapshots.forEach(s => {
if (!after || s.updatedAt.getTime() > after) {
result[s.id] = s.updatedAt.getTime();
}
result[s.id] = s.updatedAt.getTime();
});
updates.forEach(u => {
@@ -378,7 +381,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
protected async getDocSnapshot(workspaceId: string, docId: string) {
const snapshot = await this.db.snapshot.findUnique({
where: {
id_workspaceId: {
workspaceId_id: {
workspaceId,
id: docId,
},
@@ -414,7 +417,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
//
// ii. Prisma doesn't support `upsert` with additional `where` condition along side unique constraint.
// In our case, we need to manually check the `updatedAt` to avoid overriding the newer snapshot.
// where: { id_workspaceId: {}, updatedAt: { lt: updatedAt } }
// where: { workspaceId_id: {}, updatedAt: { lt: updatedAt } }
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
try {
const result: { updatedAt: Date }[] = await this.db.$queryRaw`
@@ -432,7 +435,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
// seq: true,
// },
// where: {
// id_workspaceId: {
// workspaceId_id: {
// workspaceId,
// id: guid,
// },
@@ -571,7 +574,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
seq: true,
},
where: {
id_workspaceId: {
workspaceId_id: {
workspaceId,
id: guid,
},
@@ -594,7 +597,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
seq: true,
},
where: {
id_workspaceId: {
workspaceId_id: {
workspaceId,
id: guid,
},

View File

@@ -78,7 +78,7 @@ export class Guid1698398506533 {
}),
db.snapshot.update({
where: {
id_workspaceId: {
workspaceId_id: {
id: docId.guid,
workspaceId: doc.workspaceId,
},
@@ -93,7 +93,7 @@ export class Guid1698398506533 {
// just modify the id the required one
await db.snapshot.update({
where: {
id_workspaceId: {
workspaceId_id: {
id: doc.id,
workspaceId: doc.workspaceId,
},