AidanLee has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I've got an efficiency/comprehensiveness question. I've got a list of sql tables, and a double-keyed hash (thanks btrott) which contains relationships between those tables. So here is the code that I've currently got to search/record that the relationships exist:

#Anonymous block for scoping { my( @linked_tables,@unlinked_tables) = (); #----------------------------------------------------------------- +------------------------------------ sub _get_table_relations { my( $self,$tables,$conditions ) = @_; #prep lists @unlinked_tables = keys %$tables; @linked_tables = (); my @tables_nolinks = (); #no relations to be found if there's only one table return if scalar( @unlinked_tables ) <= 1; # try to find a link for each of the supplied tables while( my $table = shift @unlinked_tables ) { next if grep { $table eq $_ } @linked_tables; push @tables_nolinks, $table unless $self->_table_relation +_helper($table,$conditions); } csWarn(class=>csErrorClass('CODE_PARAMS'),severity=>csErrorSca +le('MODERATE'), message=>'Could not find relations certain tables in th +is view.', debug=>"tables without relations:".join(',',@tables_nol +inks) ) if scalar( @tables_nolinks ); return 1; } #----------------------------------------------------------------- +------------------------------------ sub _table_relation_helper { my ($self,$table,$conditions) = @_; foreach my $table2 ( @linked_tables, @unlinked_tables ) { # a link exists if( exists $self->{_links}{$table,$table2} ) { #store the conditions of our found relationship with o +ur other conditions push @{ $conditions->{axioms} }, ( $self->{_links}{ +$table,$table2}{op} eq $conditions->{op} and ( $conditions->{op} eq ' +and' or $conditions->{op} eq 'or' )) ? @{ $self->{_ +links}{$table,$table2}{axioms} } : { %{ $self-> +{_links}{$table,$table2} } }; #first check to see if $table2 needs linking foreach my $i ( 0 .. $#unlinked_tables ) { if( $unlinked_tables[$i] eq $table2 ) { splice(@unlinked_tables,$i,1); $self->_table_relation_helper($table2,$conditi +ons) unless $table2 =~ /\s/; last; #short circuit the foreach } } #then add $table to $linked_tables push @linked_tables, $table; return 1; #short circuit } } return 0; } }

I've had to go back and add to this a couple of times as i've realized I left out certain scenarios where a relationship existed but the code couldn't find it, or where I wound up with two unjoined maps of tables when there should only be one map. So at this point I'm wondering if

Replies are listed 'Best First'.
Re: Efficiency in Mapping Relationships
by runrig (Abbot) on Jul 23, 2001 at 22:44 UTC
    While I'm still not entirely sure exactly what you're doing (some examples would help), I can't help but think that your 'linked_tables' and 'unlinked_tables' would be better off as hashes so that you wouldn't have to iterate through them quite so often:
    Anonymous block for scoping { my( %linked_tables, %unlinked_tables); #------------------------------------------ sub _get_table_relations { my( $self,$tables,$conditions ) = @_; #prep lists @unlinked_tables{keys %$tables} = undef; %linked_tables = (); my @tables_nolinks = (); #no relations to be found if there's only one table { no warnings; return if scalar( %unlinked_tables ) <= 1; } # try to find a link for each of the supplied tables for my $table ( keys %unlinked_tables ) { next if exists $linked_tables{$table}; push @tables_nolinks, $table unless $self->_table_relation_helper($table,$conditions); } csWarn(class=>csErrorClass('CODE_PARAMS'), severity=>csErrorScale('MODERATE'), message=>'Could not find relations '. 'certain tables in this view.', debug=>"tables without relations:". join(',',@tables_nolinks) ) if scalar( @tables_nolinks ); return 1; } #--------------------------------------------- sub _table_relation_helper { my ($self,$table,$conditions) = @_; foreach my $table2 (keys %linked_tables, keys %unlinked_tables) { # a link exists if( exists $self->{_links}{$table,$table2} ) { #store the conditions of our found relationship with #our other conditions push @{ $conditions->{axioms} }, ( $self->{_links}{$table,$table2}{op} eq $conditions->{op} and ( $conditions->{op} eq 'and' or $conditions->{op} eq 'or' ) ) ? @{ $self->{_links}{$table,$table2}{axioms} } : { %{ $self->{_links}{$table,$table2} } }; #first check to see if $table2 needs linking if (exists $unlinked_tables{$table2}) { delete $unlinked_tables{$table2}; $self->_table_relation_helper($table2,$conditions) unless $table2 =~ /\s/; } #then add $table to $linked_tables $linked_tables{$table} = undef; return 1; #short circuit } } return 0; } }