in reply to
Foreach loop takes a long time to process and my report times out before page is rendered
A few options mostly from what I'm seeing here.
It looks like you are precaching the entire table to a set of hashes and then sorting them and looking up each key individually. If you saved off the data to an array you could save time on the sort call and hash lookups.
An option I do use a bit is to utilize Ajax. Setup a back end process which creates the report and have the Ajax calls populate a div when the report is complete.