From ab4eb42a30a6146b0e60f3833732ca636aee642d Mon Sep 17 00:00:00 2001 From: Alvaro Urbaez Date: Mon, 26 Jun 2023 11:46:01 -0400 Subject: [PATCH] Fixing performance issue when having millions of links already generated. --- app/Helpers/LinkHelper.php | 7 +++-- ...4720_add_link_table_index_on_is_custom.php | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 database/migrations/2023_06_26_104720_add_link_table_index_on_is_custom.php diff --git a/app/Helpers/LinkHelper.php b/app/Helpers/LinkHelper.php index 5abd67528..e6cd5dedd 100644 --- a/app/Helpers/LinkHelper.php +++ b/app/Helpers/LinkHelper.php @@ -118,9 +118,12 @@ static public function findSuitableEnding() { */ $base = env('POLR_BASE'); - $link = Link::where('is_custom', 0) + /*$link = Link::where('is_custom', 0) ->orderBy('created_at', 'desc') - ->first(); + ->first();*/ + + // This change works quite faster after an index creation on is_custom + $link = Link::whereRaw('id = (SELECT max(id) FROM links WHERE is_custom = 0)')->get()->first(); if ($link == null) { $base10_val = 0; diff --git a/database/migrations/2023_06_26_104720_add_link_table_index_on_is_custom.php b/database/migrations/2023_06_26_104720_add_link_table_index_on_is_custom.php new file mode 100644 index 000000000..b7093178c --- /dev/null +++ b/database/migrations/2023_06_26_104720_add_link_table_index_on_is_custom.php @@ -0,0 +1,30 @@ +index('is_custom', 'links_is_custom_index'); + }); + + } + + public function down() + { + Schema::table('links', function (Blueprint $table) + { + $table->dropIndex('is_custom'); + }); + } +}