Php – multidimensional PHP array or multiple queries to thesql DB


I'm using the PHPExcel lib which it seems to do great jobs, but in my approach it doesn't seem to be so fast.

I need to generate a excel file with allot of sheets, data, formulas and styles(bold, color, border) and it takes me allot of computing resources and time.
I think that my approach is not so good. Which has to be the right order when is about to generate this kind of file(excel) using PHP code and datas from a mysql DB.

1) generate all the sheets with no format/style, no formula, adding datas and the formulas and style
2) generate sheet by sheet with datas, adding formulas, adding style

Is better to have all the need data from the DB in a multidimensional array and not to query the db each time I wish to write to a new sheet?

I'm using a dual core processor machine and 4GB or RAM and takes me about 1minute if the Apache doesn't crush.

Best Solution

  1. have mysql do the calculations: I once optimized a report where the original developer used loops to calculate sums rather that sql sum function, it was so badly written it exceeded time limit for php
  2. make sure your calculation SQL are optimized enough
  3. calculate then generate content