in reply to Nesting prepared DBI handles
it's quite uneffective to run inner select. You can also fall in "deep recursion" problem. Consider to use some tree implementation in SQL (for example, Joe Celko has book about that)
this one solves only this problem. it will work only on trees small enough to fit in your RAM.sub prepare_tree { my $sth = $dbh->prepare('SELECT * FROM table'); $sth->execute; my $retval = {}; while (my $hash = $sth->fetchrow_array) { push @{ $retval->{ $hash->{parent_id} } }, { %$hash }; } $sth->finish; $retval; }
|
|---|