Skip to content

A free open source IT asset/license management system

Flatten queries

snipe89ce71b350a6
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index cee843f68c..d3b1991a5a 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -1482,13 +1482,10 @@ class Asset extends Depreciable
*/
public function scopePending($query)
{
- return $query->whereHas(
- 'status', function ($query) {
- $query->where('deployable', '=', 0)
- ->where('pending', '=', 1)
- ->where('archived', '=', 0);
- }
- );
+ // Pluck IDs then whereIn — do NOT replace with whereHas. whereHas generates a correlated EXISTS per row and causes severe slowdowns in withCount contexts.
+ $ids = Statuslabel::where('deployable', 0)->where('pending', 1)->where('archived', 0)->whereNull('deleted_at')->pluck('id');
+
+ return $query->whereIn('assets.status_id', $ids->isEmpty() ? [0] : $ids);
}
/**
@@ -1538,14 +1535,11 @@ class Asset extends Depreciable
*/
public function scopeRTD($query)
{
+ // Pluck IDs then whereIn — do NOT replace with whereHas. whereHas generates a correlated EXISTS per row and causes severe slowdowns in withCount contexts.
+ $ids = Statuslabel::where('deployable', 1)->where('pending', 0)->where('archived', 0)->whereNull('deleted_at')->pluck('id');
+
return $query->whereNull('assets.assigned_to')
- ->whereHas(
- 'status', function ($query) {
- $query->where('deployable', '=', 1)
- ->where('pending', '=', 0)
- ->where('archived', '=', 0);
- }
- );
+ ->whereIn('assets.status_id', $ids->isEmpty() ? [0] : $ids);
}
/**
@@ -1556,13 +1550,10 @@ class Asset extends Depreciable
*/
public function scopeUndeployable($query)
{
- return $query->whereHas(
- 'status', function ($query) {
- $query->where('deployable', '=', 0)
- ->where('pending', '=', 0)
- ->where('archived', '=', 0);
- }
- );
+ // Pluck IDs then whereIn — do NOT replace with whereHas. whereHas generates a correlated EXISTS per row and causes severe slowdowns in withCount contexts.
+ $ids = Statuslabel::where('deployable', 0)->where('pending', 0)->where('archived', 0)->whereNull('deleted_at')->pluck('id');
+
+ return $query->whereIn('assets.status_id', $ids->isEmpty() ? [0] : $ids);
}
/**
@@ -1573,11 +1564,10 @@ class Asset extends Depreciable
*/
public function scopeNotArchived($query)
{
- return $query->whereHas(
- 'status', function ($query) {
- $query->where('archived', '=', 0);
- }
- );
+ // Pluck IDs then whereIn — do NOT replace with whereHas. whereHas generates a correlated EXISTS per row and causes severe slowdowns in withCount contexts.
+ $ids = Statuslabel::where('archived', 0)->whereNull('deleted_at')->pluck('id');
+
+ return $query->whereIn('assets.status_id', $ids->isEmpty() ? [0] : $ids);
}
/**
@@ -1740,17 +1730,16 @@ class Asset extends Depreciable
*/
public function scopeAssetsForShow($query)
{
-
+ // Pluck IDs then whereIn — do NOT replace with whereHas. whereHas generates a correlated EXISTS per row and causes severe slowdowns in withCount contexts.
if (Setting::getSettings()->show_archived_in_list != 1) {
- return $query->whereHas(
- 'status', function ($query) {
- $query->where('archived', '=', 0);
- }
- );
- } else {
- return $query;
+ $validStatusIds = Statuslabel::where('archived', 0)
+ ->whereNull('deleted_at')
+ ->pluck('id');
+
+ return $query->whereIn('assets.status_id', $validStatusIds->isEmpty() ? [0] : $validStatusIds);
}
+ return $query;
}
/**
@@ -1761,13 +1750,10 @@ class Asset extends Depreciable
*/
public function scopeArchived($query)
{
- return $query->whereHas(
- 'status', function ($query) {
- $query->where('deployable', '=', 0)
- ->where('pending', '=', 0)
- ->where('archived', '=', 1);
- }
- );
+ // Pluck IDs then whereIn — do NOT replace with whereHas. whereHas generates a correlated EXISTS per row and causes severe slowdowns in withCount contexts.
+ $ids = Statuslabel::where('deployable', 0)->where('pending', 0)->where('archived', 1)->whereNull('deleted_at')->pluck('id');
+
+ return $query->whereIn('assets.status_id', $ids->isEmpty() ? [0] : $ids);
}
/**
diff --git a/app/Models/Location.php b/app/Models/Location.php
index 638fb5d4e7..0835b82afc 100755
--- a/app/Models/Location.php
+++ b/app/Models/Location.php
@@ -170,14 +170,13 @@ class Location extends SnipeModel
*/
public function assets()
{
+ // Pluck IDs then whereIn — do NOT replace with whereHas. whereHas generates a correlated EXISTS per row and causes severe slowdowns in withCount contexts.
+ $ids = Statuslabel::where(function ($q) {
+ $q->where('deployable', 1)->orWhere('pending', 1)->orWhere('archived', 0);
+ })->whereNull('deleted_at')->pluck('id');
+
return $this->hasMany(Asset::class, 'location_id')
- ->whereHas(
- 'status', function ($query) {
- $query->where('status_labels.deployable', '=', 1)
- ->orWhere('status_labels.pending', '=', 1)
- ->orWhere('status_labels.archived', '=', 0);
- }
- );
+ ->whereIn('assets.status_id', $ids->isEmpty() ? [0] : $ids);
}
public function countAllTheThings()