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.
TypeScript appcrdteditorelectronknowledge-basemarkdownmironotesnotionnotion-alternativerustrust-langrust-languagetabletableviewwhiteboardwikiworkspace
feat: add index for snapshots (#8163)
darkskygit2a135d8a93f8
diff --git a/packages/backend/server/migrations/20240908130725_add_workspace_level_snapshot_index/migration.sql b/packages/backend/server/migrations/20240908130725_add_workspace_level_snapshot_index/migration.sqlnew file mode 100644index 0000000000..7343cf7fb7--- /dev/null+++ b/packages/backend/server/migrations/20240908130725_add_workspace_level_snapshot_index/migration.sql@@ -0,0 +1,12 @@+/*+ Warnings:++ - The primary key for the `snapshots` table will be changed. If it partially fails, the table could be left without primary key constraint.++*/+-- AlterTable+ALTER TABLE "snapshots" DROP CONSTRAINT "snapshots_pkey",+ADD CONSTRAINT "snapshots_pkey" PRIMARY KEY ("workspace_id", "guid");++-- CreateIndex+CREATE INDEX "snapshots_workspace_id_updated_at_idx" ON "snapshots"("workspace_id", "updated_at");diff --git a/packages/backend/server/schema.prisma b/packages/backend/server/schema.prismaindex 755508a9fd..ce19086e2e 100644--- a/packages/backend/server/schema.prisma+++ b/packages/backend/server/schema.prisma@@ -262,7 +262,8 @@ model Snapshot {// we need to clear all hanging updates and snapshots before enable the foreign key on workspaceId// workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)- @@id([id, workspaceId])+ @@id([workspaceId, id])+ @@index([workspaceId, updatedAt])@@map("snapshots")}diff --git a/packages/backend/server/src/core/doc/adapters/workspace.ts b/packages/backend/server/src/core/doc/adapters/workspace.tsindex 6e4e15bc6f..bd2e98127b 100644--- a/packages/backend/server/src/core/doc/adapters/workspace.ts+++ b/packages/backend/server/src/core/doc/adapters/workspace.ts@@ -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,},diff --git a/packages/backend/server/src/data/migrations/1698398506533-guid.ts b/packages/backend/server/src/data/migrations/1698398506533-guid.tsindex 095358ba93..bdc6760e06 100644--- a/packages/backend/server/src/data/migrations/1698398506533-guid.ts+++ b/packages/backend/server/src/data/migrations/1698398506533-guid.ts@@ -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 oneawait db.snapshot.update({where: {- id_workspaceId: {+ workspaceId_id: {id: doc.id,workspaceId: doc.workspaceId,},diff --git a/packages/backend/server/tests/doc/workspace.spec.ts b/packages/backend/server/tests/doc/workspace.spec.tsindex 879f45f9b1..9e2526bc08 100644--- a/packages/backend/server/tests/doc/workspace.spec.ts+++ b/packages/backend/server/tests/doc/workspace.spec.ts@@ -79,7 +79,7 @@ test('should have sequential update number', async t => {// fake the seq num is about to overflowawait db.snapshot.update({where: {- id_workspaceId: {+ workspaceId_id: {id: '2',workspaceId: '2',},@@ -273,7 +273,7 @@ test('should not update snapshot if doc is outdated', async t => {// fake the snapshot is a lot newerawait db.snapshot.update({where: {- id_workspaceId: {+ workspaceId_id: {workspaceId: '2',id: '1',},