1

Is it possible to bulk-insert data into a table using Magento?

I am working on extending the Magento import/export feature for a custom module, and noticed that each row is added one-at-a-time in a while loop. For a CSV containing thousands of rows of data, this could take a VERY long time to insert. Instead, is there a bulk-insert function laying around that takes an array of record data and bulk-inserts them?

Crude example (in reality the row data would be added to the $data array in the while loop):

$data = array(
    array('column1' => 'value', 'column2' => 'value', ...),
    array('column1' => 'value', 'column2' => 'value', ...),
    ...
);

Mage::getModel('module/model')->insert($data,true);

Where the second parameter in insert indicates a bulk-insert operation?

If not, why not? I am aware some PDOs don't have the functionality to do bulk-insert, in which case Magento would default to doing a foreach anyway. However, for PDOs that DO support bulk-insert (such as MySql) it would save a lot of time compared to touching the db for every insert. If this is not already in Magento, how would I go about implementing it? Can I have a link to the Magento feature suggestion page (Google suggests Magento Forum but I would like a second opinion before posting there).

Thanks.

2

1 Answer 1

2

Magento uses Zend_Db at it's core. However, AFAIK there is no way to do bulk inserts using Zend_Db. That said, you could create a method in your collection to accept the input as an array and then iterate over that array saving each entry.

1
  • There is no bulk-insert method in zend_db. What I did was use the query method of Zend_Db_Adapter and roll my own bulk-insert sql. I cannot remember exactly what I did it otherwise I would post an answer. Commented Feb 16, 2015 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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