Speed improvement (DBI)

Change $dbh->prepare to $dbh->prepare_cached and $sth->fetchall_arrayref to $sth->fetchrow_arrayref
Speed up (process bits)

Use a temporary table instead of hash of hashes to store the results in the internal loop. The data and then copied to the final %test hash at the end of the method.

Also, use an array of indexes instead of a counter for the loop.