FEAT: Optimize data retrieval and add new indexes for performance improvements
This commit is contained in:
@@ -145,41 +145,60 @@ export class ProjectService {
|
||||
const itemsPerPage = 10;
|
||||
const skip = (page - 1) * itemsPerPage;
|
||||
|
||||
const triggers = await prisma.trigger.findMany({
|
||||
where: { contact: { projectId: id } },
|
||||
include: {
|
||||
contact: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
const [triggers, emails] = await Promise.all([
|
||||
prisma.trigger.findMany({
|
||||
where: {
|
||||
contact: {
|
||||
projectId: id
|
||||
}
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
createdAt: true,
|
||||
contact: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
event: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
event: {
|
||||
select: {
|
||||
name: true,
|
||||
orderBy: {createdAt: 'desc'},
|
||||
take: itemsPerPage,
|
||||
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" },
|
||||
});
|
||||
|
||||
const emails = await prisma.email.findMany({
|
||||
where: { contact: { projectId: id } },
|
||||
include: {
|
||||
contact: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
orderBy: {createdAt: 'desc'},
|
||||
take: itemsPerPage,
|
||||
skip,
|
||||
}),
|
||||
]);
|
||||
|
||||
const combined = [...triggers, ...emails];
|
||||
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) {
|
||||
|
||||
@@ -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
@@ -99,6 +99,9 @@ model Event {
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index(fields: [projectId, templateId, campaignId])
|
||||
@@index([projectId])
|
||||
@@index([templateId])
|
||||
@@index([campaignId])
|
||||
@@map("events")
|
||||
}
|
||||
|
||||
@@ -155,6 +158,8 @@ model Action {
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index(fields: [projectId, templateId])
|
||||
@@index([projectId])
|
||||
@@index([templateId])
|
||||
@@map("actions")
|
||||
}
|
||||
|
||||
@@ -203,6 +208,10 @@ model Trigger {
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index(fields: [contactId, eventId, actionId, createdAt])
|
||||
@@index([contactId])
|
||||
@@index([eventId])
|
||||
@@index([actionId])
|
||||
@@index([createdAt])
|
||||
@@map("triggers")
|
||||
}
|
||||
|
||||
@@ -227,7 +236,10 @@ model Contact {
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index(fields: [projectId])
|
||||
@@index(fields: [projectId, createdAt])
|
||||
@@index([projectId])
|
||||
@@index([email])
|
||||
@@index([createdAt])
|
||||
@@map("contacts")
|
||||
}
|
||||
|
||||
@@ -259,6 +271,11 @@ model Email {
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index(fields: [projectId, actionId, campaignId, contactId, createdAt])
|
||||
@@index([projectId])
|
||||
@@index([actionId])
|
||||
@@index([campaignId])
|
||||
@@index([contactId])
|
||||
@@index([createdAt])
|
||||
@@map("emails")
|
||||
}
|
||||
|
||||
@@ -276,6 +293,9 @@ model Click {
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([emailId, createdAt])
|
||||
@@index([createdAt])
|
||||
@@index([link])
|
||||
@@index([emailId])
|
||||
@@map("clicks")
|
||||
}
|
||||
|
||||
@@ -300,6 +320,11 @@ model Task {
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([runBy, createdAt])
|
||||
@@index([actionId])
|
||||
@@index([campaignId])
|
||||
@@index([contactId])
|
||||
@@index([createdAt])
|
||||
@@index([runBy])
|
||||
@@map("tasks")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user