From 511bb0a78b200cd1e0012cbad8e25cb53cd41289 Mon Sep 17 00:00:00 2001 From: Musa <40173603+voyula@users.noreply.github.com> Date: Tue, 15 Mar 2022 00:49:08 +0300 Subject: [PATCH] Fix High CPU Usage Have 4 million rows in our media table and this query takes over than 5 seconds on 10 Core CPU server. And CPU is using %300 on during crowded hours. I think not needed execute this query for all rows, can execute for only model related rows. --- src/MediaCollections/Models/Concerns/IsSorted.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MediaCollections/Models/Concerns/IsSorted.php b/src/MediaCollections/Models/Concerns/IsSorted.php index b80f500a0..4720eadef 100644 --- a/src/MediaCollections/Models/Concerns/IsSorted.php +++ b/src/MediaCollections/Models/Concerns/IsSorted.php @@ -15,7 +15,9 @@ public function setHighestOrderNumber(): void public function getHighestOrderNumber(): int { - return (int) static::max($this->determineOrderColumnName()); + return (int) static::where('model_type', $this->model_type) + ->where('model_id', $this->model_id) + ->max($this->determineOrderColumnName()); } public function scopeOrdered(Builder $query): Builder