Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(platform): Optimise Query Indexes (#10038)

Swifty17e973a8cb80
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 605e9186e8..5ef2c2036f 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -17,7 +17,7 @@ repos:
name: Detect secrets
description: Detects high entropy strings that are likely to be passwords.
files: ^autogpt_platform/
- stages: [push]
+ stages: [pre-push]
- repo: local
# For proper type checking, all dependencies need to be up-to-date.
diff --git a/autogpt_platform/backend/backend/data/execution.py b/autogpt_platform/backend/backend/data/execution.py
index a89cb8d77c..166a53a4ed 100644
--- a/autogpt_platform/backend/backend/data/execution.py
+++ b/autogpt_platform/backend/backend/data/execution.py
@@ -1,6 +1,6 @@
import logging
from collections import defaultdict
-from datetime import datetime, timezone
+from datetime import datetime, timedelta, timezone
from enum import Enum
from multiprocessing import Manager
from typing import (
@@ -489,10 +489,15 @@ async def upsert_execution_input(
dict[str, Any]: Node input data; key is the input name, value is the input data.
"""
existing_exec_query_filter: AgentNodeExecutionWhereInput = {
- "agentNodeId": node_id,
"agentGraphExecutionId": graph_exec_id,
+ "agentNodeId": node_id,
"executionStatus": ExecutionStatus.INCOMPLETE,
- "Input": {"every": {"name": {"not": input_name}}},
+ "Input": {
+ "none": {
+ "name": input_name,
+ "time": {"gte": datetime.now(tz=timezone.utc) - timedelta(days=1)},
+ }
+ },
}
if node_exec_id:
existing_exec_query_filter["id"] = node_exec_id
@@ -715,9 +720,15 @@ async def get_latest_node_execution(
) -> NodeExecutionResult | None:
execution = await AgentNodeExecution.prisma().find_first(
where={
- "agentNodeId": node_id,
"agentGraphExecutionId": graph_eid,
- "NOT": [{"executionStatus": ExecutionStatus.INCOMPLETE}],
+ "agentNodeId": node_id,
+ "OR": [
+ {"executionStatus": ExecutionStatus.QUEUED},
+ {"executionStatus": ExecutionStatus.RUNNING},
+ {"executionStatus": ExecutionStatus.COMPLETED},
+ {"executionStatus": ExecutionStatus.TERMINATED},
+ {"executionStatus": ExecutionStatus.FAILED},
+ ],
},
order=[
{"queuedTime": "desc"},
diff --git a/autogpt_platform/backend/migrations/20250527091422_node_execution_indexes/migration.sql b/autogpt_platform/backend/migrations/20250527091422_node_execution_indexes/migration.sql
new file mode 100644
index 0000000000..c3e9b0a29f
--- /dev/null
+++ b/autogpt_platform/backend/migrations/20250527091422_node_execution_indexes/migration.sql
@@ -0,0 +1,20 @@
+-- DropIndex
+DROP INDEX "AgentNodeExecution_addedTime_idx";
+
+-- DropIndex
+DROP INDEX "AgentNodeExecution_agentGraphExecutionId_idx";
+
+-- DropIndex
+DROP INDEX "AgentNodeExecution_agentNodeId_idx";
+
+-- CreateIndex
+CREATE INDEX "AgentNodeExecution_agentGraphExecutionId_agentNodeId_execut_idx" ON "AgentNodeExecution"("agentGraphExecutionId", "agentNodeId", "executionStatus");
+
+-- CreateIndex
+CREATE INDEX "AgentNodeExecution_addedTime_queuedTime_idx" ON "AgentNodeExecution"("addedTime", "queuedTime");
+
+-- CreateIndex
+CREATE INDEX "AgentNodeExecutionInputOutput_name_time_idx" ON "AgentNodeExecutionInputOutput"("name", "time");
+
+-- CreateIndex
+CREATE INDEX "NotificationEvent_userNotificationBatchId_idx" ON "NotificationEvent"("userNotificationBatchId");
diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prisma
index 244d32a999..ae1cb28d1e 100644
--- a/autogpt_platform/backend/schema.prisma
+++ b/autogpt_platform/backend/schema.prisma
@@ -195,6 +195,8 @@ model NotificationEvent {
type NotificationType
data Json
+
+ @@index([userNotificationBatchId])
}
model UserNotificationBatch {
@@ -377,9 +379,8 @@ model AgentNodeExecution {
stats Json?
- @@index([agentGraphExecutionId])
- @@index([agentNodeId])
- @@index([addedTime])
+ @@index([agentGraphExecutionId, agentNodeId, executionStatus])
+ @@index([addedTime, queuedTime])
}
// This model describes the output of an AgentNodeExecution.
@@ -402,6 +403,8 @@ model AgentNodeExecutionInputOutput {
// Input and Output pin names are unique for each AgentNodeExecution.
@@unique([referencedByInputExecId, referencedByOutputExecId, name])
@@index([referencedByOutputExecId])
+ // Composite index for `upsert_execution_input`.
+ @@index([name, time])
}
// Webhook that is registered with a provider and propagates to one or more nodes