2

I get the number of products:

Mage::getModel('catalog/product')->getCollection()->count()

But this takes >10 seconds. Maybe because indexer is running or what ever. Can I query a table directly to get this count faster?

10

Try this.

Mage::getModel('catalog/product')->getCollection()->getSize()

When using count the full collection is loaded. For getSize() magento uses a separate sql. But this ignores any limit you set on the collection.
If you need a limit then use this:

count($collection->getAllIds());

This still executes an sql to get all the ids but it ignores the other attributes and fields.

  • But I am getting getSize() as 128 constantly even it does have 128 products.what problem with my magento?It shows only when flat product option is set as yes. – Manikandan Arunachalam Dec 2 '14 at 10:17
4

Instead of using count() use Mage::getModel('catalog/product')->getCollection()->getSize();

Note : When you use count() magento will load all the items of the collection from the database.

  • But getSize() returns unexpected value when flat category option is yes in admin. – Manikandan Arunachalam Dec 2 '14 at 10:19
2
    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection
        ->addAttributeToFilter('price', array('gt'=> 100)) //you can add any filter
        ->getSelect()
        ->reset(Zend_Db_Select::COLUMNS)
        ->columns(new Zend_Db_Expr("Count(`entity_id`) as `products_count`"));

    print_r($collection->getData());
  • This is needlessly complicated compared to the other answers. Magento supports OP's required feature out-of-the-box, no need to come up with a complicated bypass of the Model layer. – Jacques Feb 2 '17 at 13:57

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.