fix: add missing database indexes #51
2 changed files with 56 additions and 4 deletions
51
Migrations/0001_01_01_000005_add_performance_indexes.php
Normal file
51
Migrations/0001_01_01_000005_add_performance_indexes.php
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Add performance indexes for frequently queried columns (DB-002).
|
||||
*
|
||||
* Analysis per acceptance criteria:
|
||||
* - agent_sessions.session_id: the ->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');
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
9
TODO.md
9
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)
|
||||
|
||||
---
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue