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.

feat(backend): Add Missing FK indexes and remove unused & redundant indexes (#10412)

Zamil Majdyf81d7e6a56f2
diff --git a/autogpt_platform/backend/migrations/20250721081856_add_missing_fk_indexes_remove_unused_indexes/migration.sql b/autogpt_platform/backend/migrations/20250721081856_add_missing_fk_indexes_remove_unused_indexes/migration.sql
new file mode 100644
index 0000000000..86f443b56d
--- /dev/null
+++ b/autogpt_platform/backend/migrations/20250721081856_add_missing_fk_indexes_remove_unused_indexes/migration.sql
@@ -0,0 +1,109 @@
+-- DropIndex
+DROP INDEX IF EXISTS "APIKey_key_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "APIKey_prefix_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "APIKey_status_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "idx_agent_graph_execution_agent";
+
+-- DropIndex
+DROP INDEX IF EXISTS "AnalyticsDetails_type_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "AnalyticsMetrics_userId_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "IntegrationWebhook_userId_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "Profile_username_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "idx_store_listing_review_version";
+
+-- DropIndex
+DROP INDEX IF EXISTS "User_email_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "User_id_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "UserOnboarding_userId_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "idx_store_listing_version_status";
+
+-- DropIndex
+DROP INDEX IF EXISTS "idx_slv_agent";
+
+-- DropIndex
+DROP INDEX IF EXISTS "idx_store_listing_version_approved_listing";
+
+-- DropIndex
+DROP INDEX IF EXISTS "StoreListing_agentId_owningUserId_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "StoreListing_isDeleted_isApproved_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "StoreListing_isDeleted_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "StoreListingVersion_agentId_agentVersion_isDeleted_idx";
+
+-- DropIndex
+DROP INDEX IF EXISTS "idx_store_listing_approved";
+
+-- DropIndex
+DROP INDEX IF EXISTS "idx_slv_categories_gin";
+
+-- DropIndex
+DROP INDEX IF EXISTS "idx_profile_user";
+
+-- CreateIndex
+CREATE INDEX "APIKey_prefix_name_idx" ON "APIKey"("prefix", "name");
+
+-- CreateIndex
+CREATE INDEX "AgentGraph_forkedFromId_forkedFromVersion_idx" ON "AgentGraph"("forkedFromId", "forkedFromVersion");
+
+-- CreateIndex
+CREATE INDEX "AgentGraphExecution_agentPresetId_idx" ON "AgentGraphExecution"("agentPresetId");
+
+-- CreateIndex
+CREATE INDEX "AgentNodeExecution_agentNodeId_executionStatus_idx" ON "AgentNodeExecution"("agentNodeId", "executionStatus");
+
+-- CreateIndex
+CREATE INDEX "AgentPreset_agentGraphId_agentGraphVersion_idx" ON "AgentPreset"("agentGraphId", "agentGraphVersion");
+
+-- CreateIndex
+CREATE INDEX "AgentPreset_webhookId_idx" ON "AgentPreset"("webhookId");
+
+-- CreateIndex
+CREATE INDEX "LibraryAgent_agentGraphId_agentGraphVersion_idx" ON "LibraryAgent"("agentGraphId", "agentGraphVersion");
+
+-- CreateIndex
+CREATE INDEX "LibraryAgent_creatorId_idx" ON "LibraryAgent"("creatorId");
+
+-- CreateIndex
+CREATE INDEX "StoreListing_agentGraphId_agentGraphVersion_idx" ON "StoreListing"("agentGraphId", "agentGraphVersion");
+
+-- CreateIndex
+CREATE INDEX "StoreListingReview_reviewByUserId_idx" ON "StoreListingReview"("reviewByUserId");
+
+-- CreateIndex (Materialized View Performance Indexes)
+CREATE INDEX IF NOT EXISTS "idx_mv_review_stats_rating" ON "mv_review_stats" ("avg_rating" DESC);
+
+-- CreateIndex
+CREATE INDEX IF NOT EXISTS "idx_mv_review_stats_count" ON "mv_review_stats" ("review_count" DESC);
+
+-- RenameIndex (only if exists)
+DO $$
+BEGIN
+ IF EXISTS (SELECT 1 FROM pg_indexes WHERE indexname = 'analyticsDetails') THEN
+ ALTER INDEX "analyticsDetails" RENAME TO "AnalyticsDetails_userId_type_idx";
+ END IF;
+END $$;
diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prisma
index 1e8c17f753..bcc56b541a 100644
--- a/autogpt_platform/backend/schema.prisma
+++ b/autogpt_platform/backend/schema.prisma
@@ -53,9 +53,6 @@ model User {
APIKeys APIKey[]
IntegrationWebhooks IntegrationWebhook[]
NotificationBatches UserNotificationBatch[]
-
- @@index([id])
- @@index([email])
}
enum OnboardingStep {
@@ -98,8 +95,6 @@ model UserOnboarding {
userId String @unique
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
-
- @@index([userId])
}
// This model describes the Agent Graph/Flow (Multi Agent System).
@@ -135,6 +130,7 @@ model AgentGraph {
@@id(name: "graphVersionId", [id, version])
@@index([userId, isActive])
+ @@index([forkedFromId, forkedFromVersion])
}
////////////////////////////////////////////////////////////
@@ -176,6 +172,8 @@ model AgentPreset {
isDeleted Boolean @default(false)
@@index([userId])
+ @@index([agentGraphId, agentGraphVersion])
+ @@index([webhookId])
}
enum NotificationType {
@@ -248,6 +246,8 @@ model LibraryAgent {
isDeleted Boolean @default(false)
@@unique([userId, agentGraphId, agentGraphVersion])
+ @@index([agentGraphId, agentGraphVersion])
+ @@index([creatorId])
}
////////////////////////////////////////////////////////////
@@ -361,6 +361,7 @@ model AgentGraphExecution {
@@index([agentGraphId, agentGraphVersion])
@@index([userId])
@@index([createdAt])
+ @@index([agentPresetId])
}
// This model describes the execution of an AgentNode.
@@ -386,6 +387,7 @@ model AgentNodeExecution {
stats Json?
@@index([agentGraphExecutionId, agentNodeId, executionStatus])
+ @@index([agentNodeId, executionStatus])
@@index([addedTime, queuedTime])
}
@@ -415,12 +417,13 @@ model AgentNodeExecutionInputOutput {
}
model AgentNodeExecutionKeyValueData {
- userId String
- key String
- agentNodeExecutionId String
- data Json?
- createdAt DateTime @default(now())
- updatedAt DateTime? @updatedAt
+ userId String
+ key String
+ agentNodeExecutionId String
+ data Json?
+ createdAt DateTime @default(now())
+ updatedAt DateTime? @updatedAt
+
@@id([userId, key])
}
@@ -445,8 +448,6 @@ model IntegrationWebhook {
AgentNodes AgentNode[]
AgentPresets AgentPreset[]
-
- @@index([userId])
}
model AnalyticsDetails {
@@ -470,8 +471,7 @@ model AnalyticsDetails {
// Indexable field for any count based analytical measures like page order clicking, tutorial step completion, etc.
dataIndex String?
- @@index([userId, type], name: "analyticsDetails")
- @@index([type])
+ @@index([userId, type])
}
////////////////////////////////////////////////////////////
@@ -495,8 +495,6 @@ model AnalyticsMetrics {
// Link to User model
userId String
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
-
- @@index([userId])
}
////////////////////////////////////////////////////////////
@@ -582,7 +580,6 @@ model Profile {
LibraryAgents LibraryAgent[]
- @@index([username])
@@index([userId])
}
@@ -600,21 +597,13 @@ view Creator {
agent_runs Int
is_featured Boolean
- // Note: Prisma doesn't support indexes on views, but the following indexes exist in the database:
- //
- // Optimized indexes (partial indexes to reduce size and improve performance):
- // - idx_profile_user on Profile(userId)
- // - idx_store_listing_approved on StoreListing(owningUserId) WHERE isDeleted = false AND hasApprovedVersion = true
- // - idx_store_listing_version_status on StoreListingVersion(storeListingId) WHERE submissionStatus = 'APPROVED'
- // - idx_slv_categories_gin - GIN index on StoreListingVersion(categories) WHERE submissionStatus = 'APPROVED'
- // - idx_slv_agent on StoreListingVersion(agentGraphId, agentGraphVersion) WHERE submissionStatus = 'APPROVED'
- // - idx_store_listing_review_version on StoreListingReview(storeListingVersionId)
- // - idx_store_listing_version_approved_listing on StoreListingVersion(storeListingId, version) WHERE submissionStatus = 'APPROVED'
- // - idx_agent_graph_execution_agent on AgentGraphExecution(agentGraphId)
- //
// Materialized views used (refreshed every 15 minutes via pg_cron):
// - mv_agent_run_counts - Pre-aggregated agent execution counts by agentGraphId
+ // * idx_mv_agent_run_counts (UNIQUE on agentGraphId) - Primary lookup
// - mv_review_stats - Pre-aggregated review statistics (count, avg rating) by storeListingId
+ // * idx_mv_review_stats (UNIQUE on storeListingId) - Primary lookup
+ // * idx_mv_review_stats_rating (avg_rating DESC) - Sort by rating performance
+ // * idx_mv_review_stats_count (review_count DESC) - Sort by review count performance
//
// Query strategy: Uses CTEs to efficiently aggregate creator statistics leveraging materialized views
}
@@ -639,28 +628,13 @@ view StoreAgent {
rating Float
versions String[]
- // Note: Prisma doesn't support indexes on views, but the following indexes exist in the database:
- //
- // Optimized indexes (partial indexes to reduce size and improve performance):
- // - idx_store_listing_approved on StoreListing(owningUserId) WHERE isDeleted = false AND hasApprovedVersion = true
- // - idx_store_listing_version_status on StoreListingVersion(storeListingId) WHERE submissionStatus = 'APPROVED'
- // - idx_slv_categories_gin - GIN index on StoreListingVersion(categories) WHERE submissionStatus = 'APPROVED' for array searches
- // - idx_slv_agent on StoreListingVersion(agentGraphId, agentGraphVersion) WHERE submissionStatus = 'APPROVED'
- // - idx_store_listing_review_version on StoreListingReview(storeListingVersionId)
- // - idx_store_listing_version_approved_listing on StoreListingVersion(storeListingId, version) WHERE submissionStatus = 'APPROVED'
- // - idx_agent_graph_execution_agent on AgentGraphExecution(agentGraphId)
- // - idx_profile_user on Profile(userId)
- //
- // Additional indexes from earlier migrations:
- // - StoreListing_agentId_owningUserId_idx
- // - StoreListing_isDeleted_isApproved_idx (replaced by idx_store_listing_approved)
- // - StoreListing_isDeleted_idx
- // - StoreListing_agentId_key (unique on agentGraphId)
- // - StoreListingVersion_agentId_agentVersion_isDeleted_idx
- //
// Materialized views used (refreshed every 15 minutes via pg_cron):
// - mv_agent_run_counts - Pre-aggregated agent execution counts by agentGraphId
+ // * idx_mv_agent_run_counts (UNIQUE on agentGraphId) - Primary lookup
// - mv_review_stats - Pre-aggregated review statistics (count, avg rating) by storeListingId
+ // * idx_mv_review_stats (UNIQUE on storeListingId) - Primary lookup
+ // * idx_mv_review_stats_rating (avg_rating DESC) - Sort by rating performance
+ // * idx_mv_review_stats_count (review_count DESC) - Sort by review count performance
//
// Query strategy: Uses CTE for version aggregation and joins with materialized views for performance
}
@@ -748,6 +722,7 @@ model StoreListing {
@@unique([owningUserId, slug])
// Used in the view query
@@index([isDeleted, hasApprovedVersion])
+ @@index([agentGraphId, agentGraphVersion])
}
model StoreListingVersion {
@@ -821,6 +796,7 @@ model StoreListingReview {
comments String?
@@unique([storeListingVersionId, reviewByUserId])
+ @@index([reviewByUserId])
}
enum SubmissionStatus {
@@ -856,9 +832,7 @@ model APIKey {
userId String
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
- @@index([key])
- @@index([prefix])
- @@index([status])
+ @@index([prefix, name])
@@index([userId, status])
}