title: Speed up csv export when using php from mysql database query
body: <p>Ok, so i've got a web system (built on codeigniter &amp; running on mysql) that allows people to query a database of postal address data by making selections in a series of forms until they arrive at the selection that want, pretty standard stuff. They can then buy that information and download it via that system.</p>
  
  <p>The queries run very fast, but when it comes to applying that query to the database,and exporting it to csv, once the datasets get to around the 30,000 record mark (each row has around 40 columns of which about 20 are all populated with on average 20 chars of data per cell) it can take 5 or so minutes to export to csv.</p>
  
  <p>So, my question is, what is the main cause for the slowness? Is it that the resultset of data from the query is so large, that it is running into memory issues? Therefore should i allow much more memory to the process? Or, is there a much more efficient way of exporting to csv from a mysql query that i'm not doing? Should i save the contents of the query to a temp table and simply export the temp table to csv? Or am i going about this all wrong? Also, is the fact that i'm using Codeigniters Active Record for this prohibitive due to the way that it stores the resultset?</p>
  
  <p>Pseudo Code:</p>
  
  <pre><code>$query = $this-&gt;db-&gt;select('field1, field2, field3')-&gt;where_in('field1',$values_array)-&gt;get('tablename');
  $data = $this-&gt;dbutil-&gt;csv_from_result($download_query, $delimiter, $newline); // the some code to save the file
  $this-&gt;load-&gt;helper('download');
  force_download($filename, $filedata);
  </code></pre>
  
  <p>Any advice is welcome! Thank you for reading!</p>