Reputation: 43850
I am having trouble with allowing users to download a large report from the data base.
Users have the ability to browse the report on the website, i have set up a limit and a pagination for the pages so no problem there.
But now i am adding the functionality to download the whole report at once in a csv file. I receive memory error saying i have reach the maximum memory allowed, and i don't have permission on the server to increase it. Does anybody know how can make this possible? so far just getting the query which has 700k result stops my script.
ps i have search around stackoverflow and so far can't find an answer to it. I am using php and mysql to get the data.
Thanks in advance
Upvotes: 1
Views: 2141
Reputation: 49
you can append data to a file in bunch of records Something like : Get first 10000 record from mysql Write first 10000 record to file Get second 10000 record Write second 10000 record to file Then crate link and send it to user.
Upvotes: 0
Reputation: 43850
Just like @symbcbean said, the process of creating a file and appending the data with each query offset is very slow. So i came up with an alternative that i thought i should share in case someone else is facing the same issue.
I have setup a cronjob that goes through the process of creating the file with all the necessary data in it at night, since the data only change a few times a week. So now i've cleared the overhead of generating a new csv everytime someone needs to access it, instead i simply redirect to the pre-made file and the work is done!
Thank you @Gaurav & @symbcbean!
Upvotes: 0
Reputation: 48387
As per my comment on Guarav's answer you can't fetch that much data from the database in a single go - it doesn't matter how clever you are in how you manipulate it later.
You must either
how can i redirect? if i have already sent
This is trivial - if you're having problems understanding how to do this then you're already massively out of your depth.
Upvotes: 1
Reputation: 28775
Do not export whole data in a single request.
Apply some limit and offset in exporting data from database.
Like :
Upvotes: 3