I need to optimized my answer.

$start=2019-05-18 00:00:01;
$end=2019-05-18 23:59:59;

$customers = Mage::getModel('customer/customer')->getCollection()
    ->addFieldToFilter('created_at', array("gteq" => $greaterOrEqualDate))
    ->addAttributeToFilter('is_xyz_email_sent', array('neq' => 1));;

foreach ($customers as $customer){
    $lastOrder = Mage::getModel('sales/order')->getCollection()


    if($lastOrder->getCustomerEmail() && $lastOrder->getCreatedAt() >= $start && $lastOrder->getCreatedAt() <= $end) {


        if($unsubModule->getSize()==0) {
            array_push($customerArr, $lastOrder->getCustomerEmail());


This gives me right output but I need it optimized way because I m working with large database


You can try with flat sql query something like below,

select e.*,sfoi.* from sales_flat_order e
inner join sales_flat_order_item sfoi on (e.entity_id=sfoi.order_id)
where e.customer_email = '$customer->getEmail()';
  • No @Vitts this will not give exact output what I want. I tried, Can you please more explain – Inzamam Waikar Aug 19 '19 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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