From 5648ca9997e4d986799bccd877deaf72b47e48a0 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Sun, 3 Jul 2016 11:56:07 +0200 Subject: DB::Tags::dbTTTree: Use hash lookup for parent IDs Reduces page load time of the trait index from 200ms to 20ms. Also provides a slight improvement for other tag/trait tree views. --- lib/VNDB/DB/Tags.pm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/VNDB/DB/Tags.pm b/lib/VNDB/DB/Tags.pm index f3c901ab..10b45a4b 100644 --- a/lib/VNDB/DB/Tags.pm +++ b/lib/VNDB/DB/Tags.pm @@ -111,9 +111,10 @@ sub dbTTTree { $id ? {'id = ?' => $id} : {"NOT EXISTS(SELECT 1 FROM ${type}s_parents WHERE $type = id)" => 1, 'state = 2' => 1}, !$back ? ('tp.parent = tt.id', "t.id = tp.$type") : ("tp.$type = tt.id", 't.id = tp.parent') ); - for my $i (@$r) { - $i->{'sub'} = [ grep $_->{parent} == $i->{id}, @$r ]; - } + + my %pars; # parent-id -> [ child-object, .. ] + push @{$pars{$_->{parent}}}, $_ for(@$r); + $_->{'sub'} = $pars{$_->{id}} || [] for(@$r); my @r = grep !delete($_->{parent}), @$r; return $id ? $r[0]{'sub'} : \@r; } -- cgit v1.2.3