A couple of thoughts. Your top-level keynames appear to serve two purposes:
But this requires you to sort the hash to recover the ordering.
But it forces you to sort and count the values Which you've recognised by considering accumulating the counts within separate hash keys.
A better solution to both problems is to use arrays. Actually an Array of Arrays (of Arrays) of Hashes.
@structure = ( [ # Result from first query. { ... }, { ... }, ], [ Results from second query [ # first parameter { ... } { ... } ], [ # second parameter { ... }, { ... } ] ], [ # results from third query [ # first parameter { ... } ], ... ] );
You may be able to flatten the inner levels of the last two query results -- it's not clear from your post whether you need this separation.
This way, you neither need to sort to recover the ordering nor count to discover the numbers. The ordering is inherent and the counts are mantained by the individual arrays. The worst you might need to do is accumulate the total counts for a composite query:
# Total results from all parameters of query 2 # See List::Util qw[ sum ]; my $q2Total = sum @{ $structure[ 1 ] };
The second thought is that your using fetchrow_hashref in a tight loop and pushing to an array.
From the DBI pod:
If $slice is a hash reference, fetchall_arrayref uses fetchrow_hashref to fetch each row as a hash reference. If the $slice hash is empty then fetchrow_hashref() is simply called in a tight loop and the keys in the hashes have whatever name lettercase is returned by default from fetchrow_hashref. (See FetchHashKeyName attribute.) I
Using the correct varient and parameters of the fetchall_arrayref will probably run more quickly and avoid a lot of copying.
Lastly, without knowing what volumes or the nature of the data your pulling in, this maybe off target, but unless you absolutely need to have access to all this data simultaneously, and the processing that requires it is such that it cannot be done by the database itself, you probably ought to consider fetching that subset of the data you need at any given time rather than fetching it all in one big hit and storing it locally. Especially so if this is a (non-mod_perl) CGI program where your going to have to reload it for each request.
In reply to Re: Best complex structure?
by BrowserUk
in thread Best complex structure?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |