in reply to an efficient program?

Despite the large amount of information you provided, it is still not clear how your data is laid out or what type of analysis you intend to do. For some tips on how to present your problem see [How (Not) To Ask A Question].

Depending on the type of analysis you need, you should strongly consider using the capabilities of the underlying mysql database instead, and minimze the use of client-side code for database processing -- Do as much processing as possible on the database side (for the sake of speed and bandwidth). When you have your results, then you can create a pretty report with perl.