fix(backend): Add missing DB indexes (#8929)

Some table foreign key sources are not properly indexed, causing the
potential full table scan on the code queries.

### Changes 🏗️

Added DB indexes on several tables.

### Checklist 📋

#### For code changes:
- [ ] I have clearly listed my changes in the PR description
- [ ] I have made a test plan
- [ ] I have tested my changes according to the test plan:
  <!-- Put your test plan here: -->
  - [ ] ...

<details>
  <summary>Example test plan</summary>
  
  - [ ] Create from scratch and execute an agent with at least 3 blocks
- [ ] Import an agent from file upload, and confirm it executes
correctly
  - [ ] Upload agent to marketplace
- [ ] Import an agent from marketplace and confirm it executes correctly
  - [ ] Edit an agent from monitor, and confirm it executes correctly
</details>

#### For configuration changes:
- [ ] `.env.example` is updated or already compatible with my changes
- [ ] `docker-compose.yml` is updated or already compatible with my
changes
- [ ] I have included a list of my configuration changes in the PR
description (under **Changes**)

<details>
  <summary>Examples of configuration changes</summary>

  - Changing ports
  - Adding new services that need to communicate with each other
  - Secrets or environment variable changes
  - New or infrastructure changes such as databases
</details>
This commit is contained in:
Zamil Majdy
2024-12-10 04:19:22 -06:00
committed by GitHub
parent 79c0c314e2
commit 984d42234c
2 changed files with 71 additions and 10 deletions

View File

@@ -0,0 +1,41 @@
-- CreateIndex
CREATE INDEX "AgentGraph_userId_isActive_idx" ON "AgentGraph"("userId", "isActive");
-- CreateIndex
CREATE INDEX "AgentGraphExecution_agentGraphId_agentGraphVersion_idx" ON "AgentGraphExecution"("agentGraphId", "agentGraphVersion");
-- CreateIndex
CREATE INDEX "AgentGraphExecution_userId_idx" ON "AgentGraphExecution"("userId");
-- CreateIndex
CREATE INDEX "AgentNode_agentGraphId_agentGraphVersion_idx" ON "AgentNode"("agentGraphId", "agentGraphVersion");
-- CreateIndex
CREATE INDEX "AgentNode_agentBlockId_idx" ON "AgentNode"("agentBlockId");
-- CreateIndex
CREATE INDEX "AgentNode_webhookId_idx" ON "AgentNode"("webhookId");
-- CreateIndex
CREATE INDEX "AgentNodeExecution_agentGraphExecutionId_idx" ON "AgentNodeExecution"("agentGraphExecutionId");
-- CreateIndex
CREATE INDEX "AgentNodeExecution_agentNodeId_idx" ON "AgentNodeExecution"("agentNodeId");
-- CreateIndex
CREATE INDEX "AgentNodeExecutionInputOutput_referencedByOutputExecId_idx" ON "AgentNodeExecutionInputOutput"("referencedByOutputExecId");
-- CreateIndex
CREATE INDEX "AgentNodeLink_agentNodeSourceId_idx" ON "AgentNodeLink"("agentNodeSourceId");
-- CreateIndex
CREATE INDEX "AgentNodeLink_agentNodeSinkId_idx" ON "AgentNodeLink"("agentNodeSinkId");
-- CreateIndex
CREATE INDEX "AnalyticsMetrics_userId_idx" ON "AnalyticsMetrics"("userId");
-- CreateIndex
CREATE INDEX "IntegrationWebhook_userId_idx" ON "IntegrationWebhook"("userId");
-- CreateIndex
CREATE INDEX "UserBlockCredit_userId_createdAt_idx" ON "UserBlockCredit"("userId", "createdAt");

View File

@@ -21,13 +21,13 @@ model User {
integrations String @default("")
// Relations
AgentGraphs AgentGraph[]
AgentGraphExecutions AgentGraphExecution[]
IntegrationWebhooks IntegrationWebhook[]
AnalyticsDetails AnalyticsDetails[]
AnalyticsMetrics AnalyticsMetrics[]
UserBlockCredit UserBlockCredit[]
APIKeys APIKey[]
AgentGraphs AgentGraph[]
AgentGraphExecutions AgentGraphExecution[]
IntegrationWebhooks IntegrationWebhook[]
AnalyticsDetails AnalyticsDetails[]
AnalyticsMetrics AnalyticsMetrics[]
UserBlockCredit UserBlockCredit[]
APIKeys APIKey[]
@@index([id])
@@index([email])
@@ -49,10 +49,11 @@ model AgentGraph {
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
AgentNodes AgentNode[]
AgentGraphExecution AgentGraphExecution[]
AgentNodes AgentNode[]
AgentGraphExecution AgentGraphExecution[]
@@id(name: "graphVersionId", [id, version])
@@index([userId, isActive])
}
// This model describes a single node in the Agent Graph/Flow (Multi Agent System).
@@ -83,6 +84,10 @@ model AgentNode {
metadata String @default("{}")
ExecutionHistory AgentNodeExecution[]
@@index([agentGraphId, agentGraphVersion])
@@index([agentBlockId])
@@index([webhookId])
}
// This model describes the link between two AgentNodes.
@@ -101,6 +106,9 @@ model AgentNodeLink {
// Default: the data coming from the source can only be consumed by the sink once, Static: input data will be reused.
isStatic Boolean @default(false)
@@index([agentNodeSourceId])
@@index([agentNodeSinkId])
}
// This model describes a component that will be executed by the AgentNode.
@@ -147,6 +155,9 @@ model AgentGraphExecution {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
stats String? // JSON serialized object
@@index([agentGraphId, agentGraphVersion])
@@index([userId])
}
// This model describes the execution of an AgentNode.
@@ -171,6 +182,9 @@ model AgentNodeExecution {
endedTime DateTime?
stats String? // JSON serialized object
@@index([agentGraphExecutionId])
@@index([agentNodeId])
}
// This model describes the output of an AgentNodeExecution.
@@ -189,6 +203,7 @@ model AgentNodeExecutionInputOutput {
// Input and Output pin names are unique for each AgentNodeExecution.
@@unique([referencedByInputExecId, referencedByOutputExecId, name])
@@index([referencedByOutputExecId])
}
// Webhook that is registered with a provider and propagates to one or more nodes
@@ -211,6 +226,8 @@ model IntegrationWebhook {
providerWebhookId String // Webhook ID assigned by the provider
AgentNodes AgentNode[]
@@index([userId])
}
model AnalyticsDetails {
@@ -254,6 +271,8 @@ model AnalyticsMetrics {
// Link to User model
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
}
enum UserBlockCreditType {
@@ -278,6 +297,7 @@ model UserBlockCredit {
metadata Json?
@@id(name: "creditTransactionIdentifier", [transactionKey, userId])
@@index([userId, createdAt])
}
enum APIKeyPermission {
@@ -290,7 +310,7 @@ enum APIKeyPermission {
model APIKey {
id String @id @default(uuid())
name String
prefix String // First 8 chars for identification
prefix String // First 8 chars for identification
postfix String
key String @unique // Hashed key
status APIKeyStatus @default(ACTIVE)