diff --git a/database/migrations/tenant/2026_01_20_085700_add_performance_indexes_to_activity_log.php b/database/migrations/tenant/2026_01_20_085700_add_performance_indexes_to_activity_log.php new file mode 100644 index 0000000..7d40d21 --- /dev/null +++ b/database/migrations/tenant/2026_01_20_085700_add_performance_indexes_to_activity_log.php @@ -0,0 +1,45 @@ +table(config('activitylog.table_name'), function (Blueprint $table) { + // 單欄索引:事件類型(高頻過濾條件) + $table->index('event', 'idx_event'); + + // 單欄索引:批次 UUID(未來批次操作查詢) + $table->index('batch_uuid', 'idx_batch_uuid'); + + // 複合索引 1:時間 + 事件類型(最常見的組合查詢) + $table->index(['created_at', 'event'], 'idx_created_event'); + + // 複合索引 2:主體類型 + 主體 ID + 時間(查詢特定資源的操作歷史) + $table->index(['subject_type', 'subject_id', 'created_at'], 'idx_subject_created'); + + // 複合索引 3:操作者 + 時間(查詢特定使用者的操作紀錄) + $table->index(['causer_id', 'created_at'], 'idx_causer_created'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) { + $table->dropIndex('idx_event'); + $table->dropIndex('idx_batch_uuid'); + $table->dropIndex('idx_created_event'); + $table->dropIndex('idx_subject_created'); + $table->dropIndex('idx_causer_created'); + }); + } +};