Reputation: 33725
I have a LAMP website built with PHP. I have a page like http://myproject.com/report-download.php
with a button that says Export CSV
. When a user clicks on the button, they get a csv file with some report data that was originally in a MySQL database. This works great when I have only a few records. But when I have 1 million records, it takes a lot longer for the page to grab the data and create the csv file. During this CSV creation period, it takes a really long time for the user to go from one page to another on the website.
How do PHP developers normally get around the slow page load issue while the web server is still working on the CSV generation? Do I get around this with PHP threading? Or is there something I'm supposed to do to apache?
Addition
I create the csv file kinda like this
while($row = $db->getData($pageNumber))
{
add_to_file('file.csv.inprogress',array_to_csv($row));
$pageNumber++;
}
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo file_get_contents('file.csv.inprogress');
But it takes a long time to create the csv file, so during this time, the page load time is really slow fo rthe user.
Upvotes: 1
Views: 148
Reputation: 13
Depending on what version of PHP you are using, you can use caching extensions such as APC, Xcache, OPCache, Eaccelerator or Memcached. They all have similar benchmarks and up/downs.
Here is a quick guide on ups and down. * Xcache is easy to install and works great, especially with a light speed server. * APC is no longer supported by PHP 5.5 and is replaced with OPCache * Memcached is good when you have multiple servers or a load balancer.
You can also use file caching but its resource intensive and not the fastest.
Apache shouldn't have to much to to with it in most situations, but you can try NGINX and see what improvement you have. In some situations it improves speed 10 fold, its hard to say without knowing what exactly your URL is and purpose of your site.
Upvotes: 0
Reputation: 73281
I'd push the file creation to the background by running
exec('nohup php script_that_creates_csv.php');
add a DB entry when the script is finished and check the DB every n seconds for this entry using AJAX. This way you/your user will most likely not be bothered with the execution and creation of your csv.
The called script should contain code to create your csv and to insert the actual state to your DB
Upvotes: 1