Magento – How to limit the results from a Left Join in Magento

collection;magento-1.9module

I have a collection that I need to join with another collection… the problem is the table contains 125k rows and so it times out.

        //Fields
        $this->addFieldToSelect('id');
        $this->addFieldToSelect('subject');
        $this->addFieldToSelect('email_address');
        $this->addFieldToSelect('mail_id');
        $this->addFieldToSelect('date_sent');
        $this->addFieldToSelect('customer_id');

        //Get latest status
        $this->getSelect()->joinLeft(
            array('me1' => Mage::getResourceModel('someprovider_mail/event')->getMainTable()),
            'main_table.id = me1.email_id',
            'event_type as current_status'
        );

       $this->getSelect()->joinLeft(
            array('me2' => Mage::getResourceModel('someprovider_mail/event')->getMainTable()),
            '(main_table.id = me2.email_id AND (me1.timestamp < me2.timestamp OR me1.timestamp = me2.timestamp AND me1.id < me2.id))',
            false
        );

So I need to limit just what comes back in the joinLeft()… how is that possible?

Best Answer

At the end you can limit the records you want

$this->getSelect()->limit(20);
Related Topic