I have MySQL InnoDB table with 1 milion of rows and I selecting 100K rows for export. Table have around 200 columns.
What have I done so far:
-
not select all with *
SELECT column1, column2, ... FROM my_table WHERE deleted=0 -- load 100k records
-
using XMLWriter php library with flush
$writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(true); $writer->startDocument('1.0', 'UTF-8'); $writer->startElement('export'); $iterator = 0; $data = $this->getData(); foreach($adverts as $advert) { $writer->startElement('ad'); $writer->writeElement('id', $data->id); // .. other columns $writer->endElement(); // end ad if (0 == $iterator % 1000) { file_put_contents($this->getFilePath(), $writer->flush(TRUE), FILE_APPEND); } $iterator++; }
But I still have Fatal error: Allowed memory size of ... bytes exhausted
Are there any other ways how to optimize that? I think I can maybe load data from database other ways, like load only ids in first round and then select IN (10k_ids), but I don't test this idea yet.
Thank you for your opinions.
I have very similar question as in How to export HTML table with 100K records with number formatting without memory exhaust
But there is not a way how to achieve low memory consuption.