From 0eab9213c07c19d9922dcedce1f61a8d6302ce3e Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sat, 21 Dec 2019 21:31:34 +0000 Subject: [PATCH] should be faster than using IN on large lists --- .../Netdisco/DB/Result/Virtual/PortMacs.pm | 33 +++++++++++++++++++ lib/App/Netdisco/Util/PortMAC.pm | 21 +++--------- 2 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 lib/App/Netdisco/DB/Result/Virtual/PortMacs.pm diff --git a/lib/App/Netdisco/DB/Result/Virtual/PortMacs.pm b/lib/App/Netdisco/DB/Result/Virtual/PortMacs.pm new file mode 100644 index 00000000..6778bf96 --- /dev/null +++ b/lib/App/Netdisco/DB/Result/Virtual/PortMacs.pm @@ -0,0 +1,33 @@ +use utf8; +package App::Netdisco::DB::Result::Virtual::PortMacs; + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; + +__PACKAGE__->table_class('DBIx::Class::ResultSource::View'); + +__PACKAGE__->table("port_macs"); +__PACKAGE__->result_source_instance->is_virtual(1); +__PACKAGE__->result_source_instance->view_definition(<add_columns( + 'mac' => { data_type => 'macaddr' }, + 'ip' => { data_type => 'inet' }, +); + +1; diff --git a/lib/App/Netdisco/Util/PortMAC.pm b/lib/App/Netdisco/Util/PortMAC.pm index 5adf6290..348ffad2 100644 --- a/lib/App/Netdisco/Util/PortMAC.pm +++ b/lib/App/Netdisco/Util/PortMAC.pm @@ -33,22 +33,11 @@ sub get_port_macs { my $port_macs = {}; return {} unless defined $fw_mac_list and ref [] eq ref $fw_mac_list; - my $dp_macs - = schema('netdisco')->resultset('DevicePort') - ->search( { mac => { '-in' => $fw_mac_list } }, - { select => [ 'mac', 'ip' ], - group_by => [ 'mac', 'ip' ] } ); - my $dp_cursor = $dp_macs->cursor; - while ( my @vals = $dp_cursor->next ) { - $port_macs->{ $vals[0] } = $vals[1]; - } - - my $d_macs - = schema('netdisco')->resultset('Device') - ->search( { mac => { '-in' => $fw_mac_list } }, - { select => [ 'mac', 'ip' ] } ); - my $d_cursor = $d_macs->cursor; - while ( my @vals = $d_cursor->next ) { + my $macs + = schema('netdisco')->resultset('Virtual::PortMacs')->search({}, + { bind => [$fw_mac_list], select => [ 'mac', 'ip' ], group_by => [ 'mac', 'ip' ] } ); + my $cursor = $macs->cursor; + while ( my @vals = $cursor->next ) { $port_macs->{ $vals[0] } = $vals[1]; }