From 23682b3ffe86e96b3b09fd9556eef1f831b0161e Mon Sep 17 00:00:00 2001 From: sky121113 Date: Tue, 20 Jan 2026 09:17:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=8A=9F=E8=83=BD=EF=BC=9A=E7=82=BA?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E7=B4=80=E9=8C=84=E8=B3=87=E6=96=99=E8=A1=A8?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=95=88=E8=83=BD=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...dd_performance_indexes_to_activity_log.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 database/migrations/tenant/2026_01_20_085700_add_performance_indexes_to_activity_log.php 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'); + }); + } +};