I need a suggestion in magento.

I have created custom model collection.it has around 50,000 rows in testimonial_record. I want to fetch simply all data.

Which following query is best to fetch fast data ?

Technique 1:

$resource = Mage::getSingleton('core/resource');

        $readConnection = $resource->getConnection('core_read');

$query = 'SELECT * FROM ' . $resource->getTableName('testimonial_record');        

        $results = $readConnection->fetchAll($query);

Technique 2:

$collection = Mage::getModel('testimonial/record')->getCollection();

Which one above technique is best to fetch fast bulk data ? OR any other query to fetch fast data from custom table ?


Whenever you deal with database queries you should always use collections or resource model.

It's best practice to avoid writing direct SQL queries like technique #1 elsewhere than collection / resource model.

In terms of performance I reckon the collection do a little bit more than a simple fetchAll but best bet is to test. See the results between the two techniques in terms of speed and memory used. Also using collections has the big benefit of providing you direct Magento objects to deal with which is more convenient.

Also if you want to speed up the process I suggest you have a look at the walk iterator which will consume way less memory than looping through a collection: http://inchoo.net/magento/working-with-large-magento-collections/

|improve this answer|||||
  • I have test data for both query.In test result direct sql query is faster than collection so why we use collection in magento ? – Niraj Patel Jul 17 '16 at 15:44
  • @test to be able to access the object functionalities directly without loading them after the direct SQL queries I suggest you have a look here it'll give you a good explanation on how it works and why we use it: alanstorm.com/magento_collections – Raphael at Digital Pianism Jul 17 '16 at 16:15

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.