Improve the efficiency of some queries (#13974)

* Improve the efficiency of some queries
Mostly by switching from whereIn to whereIntegerInRaw.
This inserts integers directly into the query instead of using placeholders (also escapes them)

also remove extra json_encode/json_decode in PingCheck

* Fix return types

Probably will result in some missing baseline exceptions.

* Update PingCheck.php

* whitespace
This commit is contained in:
Tony Murray 2022-05-16 02:57:58 -05:00 committed by GitHub
parent a7147a17e1
commit 5076deccf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 31 additions and 29 deletions

View File

@ -177,7 +177,7 @@ class PermissionsCache
// if we don't have a map for this user yet, populate it.
if (! isset($this->deviceGroupMap[$user_id])) {
$this->deviceGroupMap[$user_id] = DB::table('device_group_device')
->whereIn('device_id', $this->devicesForUser($user))
->whereIntegerInRaw('device_id', $this->devicesForUser($user))
->distinct('device_group_id')
->pluck('device_group_id');
}

View File

@ -218,7 +218,7 @@ class Component
public function setComponentPrefs($device_id, $updated)
{
$updated = Arr::wrap($updated);
\App\Models\Component::whereIn('id', array_keys($updated))
\App\Models\Component::whereIntegerInRaw('id', array_keys($updated))
->with('prefs')
->get()
->each(function (\App\Models\Component $component) use ($device_id, $updated) {

View File

@ -72,7 +72,7 @@ class Nac implements Module
$delete = $existing_entries->diffKeys($nac_entries)->pluck('ports_nac_id');
if ($delete->isNotEmpty()) {
$count = PortsNac::query()->whereIn('ports_nac_id', $delete)->delete();
$count = PortsNac::query()->whereIntegerInRaw('ports_nac_id', $delete)->delete();
d_echo('Deleted ' . $count, str_repeat('-', $count));
}
}

View File

@ -83,19 +83,19 @@ class FdbTablesController extends TableController
case 'mac':
return $query->where('ports_fdb.mac_address', 'like', $mac_search);
case 'vlan':
return $query->whereIn('ports_fdb.vlan_id', $this->findVlans($search));
return $query->whereIntegerInRaw('ports_fdb.vlan_id', $this->findVlans($search));
case 'dnsname':
$search = gethostbyname($search);
// no break
case 'ip':
return $query->whereIn('ports_fdb.mac_address', $this->findMacs($search));
case 'description':
return $query->whereIn('ports_fdb.port_id', $this->findPorts($search));
return $query->whereIntegerInRaw('ports_fdb.port_id', $this->findPorts($search));
default:
return $query->where(function ($query) use ($search, $mac_search) {
$query->where('ports_fdb.mac_address', 'like', $mac_search)
->orWhereIn('ports_fdb.port_id', $this->findPorts($search))
->orWhereIn('ports_fdb.vlan_id', $this->findVlans($search))
->orWhereIntegerInRaw('ports_fdb.port_id', $this->findPorts($search))
->orWhereIntegerInRaw('ports_fdb.vlan_id', $this->findVlans($search))
->orWhereIn('ports_fdb.mac_address', $this->findMacs($search));
});
}
@ -193,9 +193,9 @@ class FdbTablesController extends TableController
/**
* @param string $ip
* @return Builder
* @return \Illuminate\Support\Collection
*/
protected function findMacs($ip)
protected function findMacs($ip): \Illuminate\Support\Collection
{
$port_id = \Request::get('port_id');
$device_id = \Request::get('device_id');
@ -212,9 +212,9 @@ class FdbTablesController extends TableController
/**
* @param string $vlan
* @return Builder
* @return \Illuminate\Support\Collection
*/
protected function findVlans($vlan)
protected function findVlans($vlan): \Illuminate\Support\Collection
{
$port_id = \Request::get('port_id');
$device_id = \Request::get('device_id');
@ -233,9 +233,9 @@ class FdbTablesController extends TableController
/**
* @param string $ifAlias
* @return Builder
* @return \Illuminate\Support\Collection
*/
protected function findPorts($ifAlias)
protected function findPorts($ifAlias): \Illuminate\Support\Collection
{
$port_id = \Request::get('port_id');
$device_id = \Request::get('device_id');
@ -252,9 +252,9 @@ class FdbTablesController extends TableController
/**
* @param string $mac_address
* @return \Illuminate\Support\Collection
* @return array
*/
protected function findIps($mac_address)
protected function findIps($mac_address): array
{
if (! isset($this->ipCache[$mac_address])) {
$ips = Ipv4Mac::where('mac_address', $mac_address)

View File

@ -145,7 +145,7 @@ class GraphController extends WidgetController
}
$data['service_text'] = isset($service) ? $service->device->displayName() . ' - ' . $service->service_type . ' (' . $service->service_desc . ')' : __('Service does not exist');
$data['graph_ports'] = Port::whereIn('port_id', $data['graph_ports'])
$data['graph_ports'] = Port::whereIntegerInRaw('port_id', $data['graph_ports'])
->select('ports.device_id', 'port_id', 'ifAlias', 'ifName', 'ifDescr')
->with(['device' => function ($query) {
$query->select('device_id', 'hostname', 'sysName');

View File

@ -161,11 +161,13 @@ class PingCheck implements ShouldQueue
->orderBy('max_depth');
if ($this->groups) {
$query->whereIn('poller_group', $this->groups);
$query->whereIntegerInRaw('poller_group', $this->groups);
}
$this->devices = $query->get()->keyBy(function ($device) {
return Device::pollerTarget(json_decode(json_encode($device), true));
/** @var Device $device */
return $device->overwrite_ip ?: $device->hostname;
});
// working collections

View File

@ -70,7 +70,7 @@ abstract class BaseModel extends Model
$table = $this->getTable();
}
return $query->whereIn("$table.device_id", \Permissions::devicesForUser($user));
return $query->whereIntegerInRaw("$table.device_id", \Permissions::devicesForUser($user));
}
/**
@ -92,8 +92,8 @@ abstract class BaseModel extends Model
}
return $query->where(function ($query) use ($table, $user) {
return $query->whereIn("$table.port_id", \Permissions::portsForUser($user))
->orWhereIn("$table.device_id", \Permissions::devicesForUser($user));
return $query->whereIntegerInRaw("$table.port_id", \Permissions::portsForUser($user))
->orWhereIntegerInRaw("$table.device_id", \Permissions::devicesForUser($user));
});
}
}

View File

@ -210,7 +210,7 @@ class Device extends BaseModel
if ($this->groups->isNotEmpty()) {
$query->orWhereHas('deviceGroups', function (Builder $query) {
$query->whereIn('alert_schedulables.alert_schedulable_id', $this->groups->pluck('id'));
$query->whereIntegerInRaw('alert_schedulables.alert_schedulable_id', $this->groups->pluck('id'));
});
}

View File

@ -89,7 +89,7 @@ class DeviceGroup extends BaseModel
return $query;
}
return $query->whereIn('id', Permissions::deviceGroupsForUser($user));
return $query->whereIntegerInRaw('id', Permissions::deviceGroupsForUser($user));
}
public function scopeInServiceTemplate($query, $serviceTemplate)

View File

@ -155,7 +155,7 @@ class Location extends Model
->whereNotNull('location_id')
->pluck('location_id');
return $query->whereIn('id', $ids);
return $query->whereIntegerInRaw('id', $ids);
}
public function scopeInDeviceGroup($query, $deviceGroup)

View File

@ -213,7 +213,7 @@ class User extends Authenticatable
{
// pseudo relation
return Device::query()->when(! $this->hasGlobalRead(), function ($query) {
return $query->whereIn('device_id', Permissions::devicesForUser($this));
return $query->whereIntegerInRaw('device_id', Permissions::devicesForUser($this));
});
}

View File

@ -190,7 +190,7 @@ class DeviceObserver
// a parent attached to this device
// update the parent's max depth incase it used to be standalone
Device::whereIn('device_id', $pivotIds)->get()->each->validateStandalone();
Device::whereIntegerInRaw('device_id', $pivotIds)->get()->each->validateStandalone();
// make sure this device's max depth is updated
$device->updateMaxDepth();
@ -201,7 +201,7 @@ class DeviceObserver
$device->validateStandalone();
// make sure the child's max depth is updated
Device::whereIn('device_id', $pivotIds)->get()->each->updateMaxDepth();
Device::whereIntegerInRaw('device_id', $pivotIds)->get()->each->updateMaxDepth();
}
}

View File

@ -80,7 +80,7 @@ if (! empty($insert)) {
echo PHP_EOL;
}
DB::table('ports_fdb')->whereIn('ports_fdb_id', $update_time_only)->update(['updated_at' => $now]);
DB::table('ports_fdb')->whereIntegerInRaw('ports_fdb_id', $update_time_only)->update(['updated_at' => $now]);
//We do not delete anything here, as daily.sh will take care of the cleaning.

View File

@ -961,7 +961,7 @@ foreach ($ports as $port) {
} //end port update
// Update the poll_time, poll_prev and poll_period of all ports in an unique request
$updated = DB::table('ports')->whereIn('port_id', $globally_updated_port_ids)->update($device_global_ports);
$updated = DB::table('ports')->whereIntegerInRaw('port_id', $globally_updated_port_ids)->update($device_global_ports);
d_echo("$updated updated\n");