Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FredericMartinez/f970bbadf38bb39c63142e9fd06a0fce to your computer and use it in GitHub Desktop.
Save FredericMartinez/f970bbadf38bb39c63142e9fd06a0fce to your computer and use it in GitHub Desktop.
Use unbuffered SQL queries with Magento to reduce memory usage on large data sets.
<?php
require_once 'abstract.php';
class Cmtickle_Demo_Shell_Tool extends Mage_Shell_Abstract
{
private $_readConnection = null;
protected function _getReadConnection()
{
if(null === $this->_readConnection) {
$this->_readConnection = Mage::getSingleton('core/resource')->getConnection('core_read');
$this->_readConnection->getConnection()->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
}
return $this->_readConnection;
}
protected function _getStatement($query, $conn = null)
{
if ($query instanceof Zend_Db_Statement_Interface) {
return $query;
}
if ($query instanceof Zend_Db_Select) {
return $query->query();
}
if (is_string($query)) {
if (!$conn instanceof Zend_Db_Adapter_Abstract) {
Mage::throwException(Mage::helper('core')->__('Invalid connection'));
}
return $conn->query($query);
}
Mage::throwException(Mage::helper('core')->__('Invalid query'));
}
private function _doProcessing()
{
$modelClass = Mage::getModel('sales/order_address');
$modelCollection = $modelClass->getCollection()->addAttributeToSelect('*');
$identityField = $modelClass->getResource()->getIdFieldName();
$query = $modelCollection->getSelect();
Mage::getSingleton('core/resource_iterator')->walk(
$query,
array(array($this, 'collectionCallback')),
array('id_field' => $identityField),
$this->_getReadConnection()
);
// This needs to happen because we're using unbuffered queries.
$this->_getStatement($query, $this->_getReadConnection())->closeCursor();
}
public function collectionCallback($args)
{
$rowId = $args['row'][$args['id_field']];
$row = $args['row'];
//
// Do your required task with the data set here.
//
}
public function run()
{
$this->_doProcessing();
}
}
$tool = new Cmtickle_Demo_Shell_Tool();
$tool->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment