vote up 1 vote down
star

Hi there
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
3)....

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.

flag
add comment

3 Answers:

vote up 0 vote down

Database queries, even ones that return like 30,000 rows, generally only take a fraction of a second. With a proc and RAM like that, it's probably slowing down at IO. I'd be willing to bet that most of the time is being spent on writing the file(s).

That being said, I would suggest having all the data up front. At least then, you're reading from memory. I'm not sure if MySQL loads everything into memory with a pointer to the current row, or if it only loads the current row.

link|flag
add comment
vote up 0 vote down

With a minute processing time your dataset doesn't sound large enough to require partitioning of the result set.

So with that assumed, loading it all into memory would be fine.

However, mysql caches the query anyway, so successive duplicate queries shouldn't be a large issue either.

It does sound like IO write to file on disk is your slow point. I don't know much about how styles and formating works for xls files but I'm pretty sure you could reduce a lot of pointless writes by reducing formatting or organizing it so that you are not doing any in-line formating, if thats even possible.

The best thing I would advise is optimizing your query. That is usually the first optimizing place I look with this type of issue. If you could edit your post and include the sql I could have a look. (feel free to snip&strip sensitive information)

link|flag
add comment
vote up 0 vote down

I would suggest profiling your PHP code to find out where the most time is spent. You can find a step-by-step tutorial about using XDebug to profile PHP conde on my blog. Then try to optimize the parts which consume the most time / are called the most frequently. One of the best optimizations you can do is to replace PHP methods by built-in functions if possible, because they are orders of magnitude faster.

link|flag
add comment

Your Answer:

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.