I am trying to login a customer automatically on the basis of a custom attribute which I get from my URL. To auto login the customer, I get the ID of customer first and then use to login the user. But as I am start to learn more about Magento, I can see in my code that my query to get customer detail will fetch all details about the customer as a collection, and the collection will be huge and might also create memory leaks. Can anyone please suggest me how to keep it optimized, I have been searching but have not found anything yet.

    $customer = Mage::getModel('customer/customer')
            ->addAttributeToFilter('website_id', WEB_ID)
            ->addAttributeToFilter('custom_attribute', $custom_attribute)

    if (is_object($customer)) {
        $customer_data = $customer->getData();

Should I go for core MySql query to fetch the entity_id, because the above fetches a lot of data.


Ultimately to adhere to Magento's ORM framework its best to go through the Customer models to get what you need. As long as you are not doing things like foreach loops with getModel()->load() then you shouldn't need to worry too much about about memory leaks.

Magento's ORM will generate some rather large queries at times depending upon your collection requests.

Querying upon the read/write adapters with raw SQL is normally best when you absolutely need to do such, such as very large specific queries in your Resource Models. I wouldn't think in such of a scenario as yours you won't gain much, and would just use the collections.

If you do end up needing to iterator over large collections of data its best to use the Mage_Core_Model_Resource_Iterator class to avoid memory leaks or running into out of memory issues.

Further reading:

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.