FEAT: Optimize data retrieval and add new indexes for performance improvements

This commit is contained in:
Dries Augustyns
2025-08-22 14:17:21 +02:00
parent 179250d74e
commit 51b40cc069
3 changed files with 146 additions and 28 deletions
+31 -12
View File
@@ -145,9 +145,16 @@ 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: {
projectId: id
}
},
select: {
id: true,
createdAt: true,
contact: { contact: {
select: { select: {
id: true, id: true,
@@ -160,12 +167,21 @@ export class ProjectService {
}, },
}, },
}, },
orderBy: { createdAt: "desc" }, orderBy: {createdAt: 'desc'},
}); take: itemsPerPage,
skip,
const emails = await prisma.email.findMany({ }),
where: { contact: { projectId: id } }, prisma.email.findMany({
include: { where: {
contact: {
projectId: id
}
},
select: {
id: true,
createdAt: true,
messageId: true,
status: true,
contact: { contact: {
select: { select: {
id: true, id: true,
@@ -173,13 +189,16 @@ export class ProjectService {
}, },
}, },
}, },
orderBy: { createdAt: "desc" }, orderBy: {createdAt: 'desc'},
}); take: itemsPerPage,
skip,
}),
]);
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")
} }