Merge pull request #208 from useplunk/dev-driaug-memory-improvements

FEAT: Optimize data retrieval and add new indexes for performance improvements
This commit is contained in:
Dries Augustyns
2025-08-22 14:22:10 +02:00
committed by GitHub
4 changed files with 147 additions and 29 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "plunk", "name": "plunk",
"version": "1.1.0", "version": "1.2.0",
"private": true, "private": true,
"license": "agpl-3.0", "license": "agpl-3.0",
"workspaces": { "workspaces": {
+46 -27
View File
@@ -145,41 +145,60 @@ export class ProjectService {
const itemsPerPage = 10; const itemsPerPage = 10;
const skip = (page - 1) * itemsPerPage; const skip = (page - 1) * itemsPerPage;
const triggers = await prisma.trigger.findMany({ const [triggers, emails] = await Promise.all([
where: { contact: { projectId: id } }, prisma.trigger.findMany({
include: { where: {
contact: { contact: {
select: { projectId: id
id: true, }
email: true, },
select: {
id: true,
createdAt: true,
contact: {
select: {
id: true,
email: true,
},
},
event: {
select: {
name: true,
},
}, },
}, },
event: { orderBy: {createdAt: 'desc'},
select: { take: itemsPerPage,
name: true, skip,
}),
prisma.email.findMany({
where: {
contact: {
projectId: id
}
},
select: {
id: true,
createdAt: true,
messageId: true,
status: true,
contact: {
select: {
id: true,
email: true,
},
}, },
}, },
}, orderBy: {createdAt: 'desc'},
orderBy: { createdAt: "desc" }, take: itemsPerPage,
}); skip,
}),
const emails = await prisma.email.findMany({ ]);
where: { contact: { projectId: id } },
include: {
contact: {
select: {
id: true,
email: true,
},
},
},
orderBy: { createdAt: "desc" },
});
const combined = [...triggers, ...emails]; const combined = [...triggers, ...emails];
combined.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()); combined.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
return combined.slice(skip, skip + itemsPerPage); return combined.slice(0, itemsPerPage);
} }
public static usage(id: string) { public static usage(id: string) {
@@ -0,0 +1,74 @@
-- CreateIndex
CREATE INDEX "actions_projectId_idx" ON "actions"("projectId");
-- CreateIndex
CREATE INDEX "actions_templateId_idx" ON "actions"("templateId");
-- CreateIndex
CREATE INDEX "clicks_createdAt_idx" ON "clicks"("createdAt");
-- CreateIndex
CREATE INDEX "clicks_link_idx" ON "clicks"("link");
-- CreateIndex
CREATE INDEX "clicks_emailId_idx" ON "clicks"("emailId");
-- CreateIndex
CREATE INDEX "contacts_projectId_createdAt_idx" ON "contacts"("projectId", "createdAt");
-- CreateIndex
CREATE INDEX "contacts_email_idx" ON "contacts"("email");
-- CreateIndex
CREATE INDEX "contacts_createdAt_idx" ON "contacts"("createdAt");
-- CreateIndex
CREATE INDEX "emails_projectId_idx" ON "emails"("projectId");
-- CreateIndex
CREATE INDEX "emails_actionId_idx" ON "emails"("actionId");
-- CreateIndex
CREATE INDEX "emails_campaignId_idx" ON "emails"("campaignId");
-- CreateIndex
CREATE INDEX "emails_contactId_idx" ON "emails"("contactId");
-- CreateIndex
CREATE INDEX "emails_createdAt_idx" ON "emails"("createdAt");
-- CreateIndex
CREATE INDEX "events_projectId_idx" ON "events"("projectId");
-- CreateIndex
CREATE INDEX "events_templateId_idx" ON "events"("templateId");
-- CreateIndex
CREATE INDEX "events_campaignId_idx" ON "events"("campaignId");
-- CreateIndex
CREATE INDEX "tasks_actionId_idx" ON "tasks"("actionId");
-- CreateIndex
CREATE INDEX "tasks_campaignId_idx" ON "tasks"("campaignId");
-- CreateIndex
CREATE INDEX "tasks_contactId_idx" ON "tasks"("contactId");
-- CreateIndex
CREATE INDEX "tasks_createdAt_idx" ON "tasks"("createdAt");
-- CreateIndex
CREATE INDEX "tasks_runBy_idx" ON "tasks"("runBy");
-- CreateIndex
CREATE INDEX "triggers_contactId_idx" ON "triggers"("contactId");
-- CreateIndex
CREATE INDEX "triggers_eventId_idx" ON "triggers"("eventId");
-- CreateIndex
CREATE INDEX "triggers_actionId_idx" ON "triggers"("actionId");
-- CreateIndex
CREATE INDEX "triggers_createdAt_idx" ON "triggers"("createdAt");
+26 -1
View File
@@ -99,6 +99,9 @@ model Event {
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@@index(fields: [projectId, templateId, campaignId]) @@index(fields: [projectId, templateId, campaignId])
@@index([projectId])
@@index([templateId])
@@index([campaignId])
@@map("events") @@map("events")
} }
@@ -155,6 +158,8 @@ model Action {
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@@index(fields: [projectId, templateId]) @@index(fields: [projectId, templateId])
@@index([projectId])
@@index([templateId])
@@map("actions") @@map("actions")
} }
@@ -203,6 +208,10 @@ model Trigger {
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@@index(fields: [contactId, eventId, actionId, createdAt]) @@index(fields: [contactId, eventId, actionId, createdAt])
@@index([contactId])
@@index([eventId])
@@index([actionId])
@@index([createdAt])
@@map("triggers") @@map("triggers")
} }
@@ -227,7 +236,10 @@ model Contact {
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@@index(fields: [projectId]) @@index(fields: [projectId, createdAt])
@@index([projectId])
@@index([email])
@@index([createdAt])
@@map("contacts") @@map("contacts")
} }
@@ -259,6 +271,11 @@ model Email {
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@@index(fields: [projectId, actionId, campaignId, contactId, createdAt]) @@index(fields: [projectId, actionId, campaignId, contactId, createdAt])
@@index([projectId])
@@index([actionId])
@@index([campaignId])
@@index([contactId])
@@index([createdAt])
@@map("emails") @@map("emails")
} }
@@ -276,6 +293,9 @@ model Click {
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@@index([emailId, createdAt]) @@index([emailId, createdAt])
@@index([createdAt])
@@index([link])
@@index([emailId])
@@map("clicks") @@map("clicks")
} }
@@ -300,6 +320,11 @@ model Task {
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@@index([runBy, createdAt]) @@index([runBy, createdAt])
@@index([actionId])
@@index([campaignId])
@@index([contactId])
@@index([createdAt])
@@index([runBy])
@@map("tasks") @@map("tasks")
} }