From bbf921d760fde850b07a68b33cd036ebdeae8336 Mon Sep 17 00:00:00 2001 From: Owen Conti Date: Thu, 5 May 2022 11:31:31 -0600 Subject: [PATCH] feat: Add indices to foreign key columns --- .../migrations/2019_12_08_100000_create_analytics_table.php | 4 ++++ .../2020_04_06_100000_add_user_id_to_analytics_table.php | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/database/migrations/2019_12_08_100000_create_analytics_table.php b/src/database/migrations/2019_12_08_100000_create_analytics_table.php index b3b3fa7..4f87bb5 100644 --- a/src/database/migrations/2019_12_08_100000_create_analytics_table.php +++ b/src/database/migrations/2019_12_08_100000_create_analytics_table.php @@ -31,6 +31,8 @@ public function up() ->references('id') ->on(ServerAnalytics::getAnalyticsDataTable()); $table->morphs('relation'); + + $table->index('analytics_id'); }); Schema::create(ServerAnalytics::getAnalyticsMetaTable(), function (Blueprint $table) { @@ -41,6 +43,8 @@ public function up() ->on(ServerAnalytics::getAnalyticsDataTable()); $table->string('key'); $table->text('value')->nullable(); + + $table->index(['analytics_id', 'key', 'value']); }); } diff --git a/src/database/migrations/2020_04_06_100000_add_user_id_to_analytics_table.php b/src/database/migrations/2020_04_06_100000_add_user_id_to_analytics_table.php index d34512c..8236124 100644 --- a/src/database/migrations/2020_04_06_100000_add_user_id_to_analytics_table.php +++ b/src/database/migrations/2020_04_06_100000_add_user_id_to_analytics_table.php @@ -14,12 +14,14 @@ public function up() Schema::table(ServerAnalytics::getAnalyticsDataTable(), function (Blueprint $table) { $table->unsignedBigInteger('user_id')->nullable()->after('id'); $table->foreign('user_id')->references('id')->on('users'); + $table->index('user_id'); }); } public function down() { Schema::table(ServerAnalytics::getAnalyticsDataTable(), function (Blueprint $table) { + $table->dropIndex('user_id'); $table->dropForeign(['user_id']); $table->dropColumn('user_id'); });