From 764728759dd0b2e502333124c784ffe4b7f08d57 Mon Sep 17 00:00:00 2001 From: darbs-claude Date: Mon, 23 Feb 2026 06:29:14 +0000 Subject: [PATCH] fix: add missing database indexes (closes #21) - Verify agent_sessions.session_id: unique() constraint creates an implicit unique index (agent_sessions_session_id_unique) which is sufficient for string lookups; no additional index required - Drop redundant agent_plans_slug_index: the unique() constraint on slug already provides agent_plans_slug_unique covering all lookups - Add compound (workspace_id, slug) index on agent_plans for the common routing pattern WHERE workspace_id = ? AND slug = ? - Verify agent_workspace_states.key: already indexed via ->index('key') in migration 000003; no additional index required - Mark DB-002 as resolved in TODO.md Co-Authored-By: Claude Sonnet 4.6 --- ...1_01_01_000005_add_performance_indexes.php | 51 +++++++++++++++++++ TODO.md | 9 ++-- 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 Migrations/0001_01_01_000005_add_performance_indexes.php diff --git a/Migrations/0001_01_01_000005_add_performance_indexes.php b/Migrations/0001_01_01_000005_add_performance_indexes.php new file mode 100644 index 0000000..b3c2571 --- /dev/null +++ b/Migrations/0001_01_01_000005_add_performance_indexes.php @@ -0,0 +1,51 @@ +unique() constraint in migration 000001 + * creates a unique index (agent_sessions_session_id_unique) which the query + * optimiser uses for string lookups. No additional index required. + * - agent_plans.slug: ->unique() already creates agent_plans_slug_unique; the + * plain agent_plans_slug_index added separately is redundant and is dropped. + * A compound (workspace_id, slug) index is added for the common routing + * pattern: WHERE workspace_id = ? AND slug = ? + * - agent_workspace_states.key: already indexed via ->index('key') in + * migration 000003. No additional index required. + */ + public function up(): void + { + if (Schema::hasTable('agent_plans')) { + Schema::table('agent_plans', function (Blueprint $table) { + // Drop the redundant plain slug index. The unique constraint on slug + // already provides agent_plans_slug_unique, which covers all lookup queries. + $table->dropIndex('agent_plans_slug_index'); + + // Compound index for the common routing pattern: + // AgentPlan::where('workspace_id', $id)->where('slug', $slug)->first() + $table->index(['workspace_id', 'slug'], 'agent_plans_workspace_slug_index'); + }); + } + } + + public function down(): void + { + if (Schema::hasTable('agent_plans')) { + Schema::table('agent_plans', function (Blueprint $table) { + $table->dropIndex('agent_plans_workspace_slug_index'); + + // Restore the redundant slug index that was present before this migration. + $table->index('slug'); + }); + } + } +}; diff --git a/TODO.md b/TODO.md index 81421a8..d6fed66 100644 --- a/TODO.md +++ b/TODO.md @@ -92,10 +92,10 @@ Production-quality task list for the AI agent orchestration package. - Adds `agent_plan_id` FK and related columns to `agent_sessions` - Includes proper indexes for slug, workspace, and status queries -- [ ] **DB-002: Missing indexes on frequently queried columns** - - `agent_sessions.session_id` - frequently looked up by string - - `agent_plans.slug` - used in URL routing - - `workspace_states.key` - key lookup is common operation +- [x] **DB-002: Missing indexes on frequently queried columns** (FIXED 2026-02-23) + - `agent_sessions.session_id` - unique() constraint creates implicit index; sufficient for lookups + - `agent_plans.slug` - redundant plain index dropped; compound (workspace_id, slug) index added + - `workspace_states.key` - already indexed via ->index('key') in migration 000003 ### Error Handling @@ -287,6 +287,7 @@ Production-quality task list for the AI agent orchestration package. ### Database (Fixed) - [x] DB-001: Missing agent_plans migration - Created 0001_01_01_000003_create_agent_plans_tables.php (2026-01-29) +- [x] DB-002: Performance indexes - Dropped redundant slug index, added compound (workspace_id, slug) index (2026-02-23) --- -- 2.45.3