Product Collection Takes too much time
-
16-10-2019 - |
Question
I have the following code.
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect(array('featuredcategory','featured', 'price','name','small_image','frontend_name'));
$collection->addFieldToFilter('type_id', 'configurable');
This takes about 5 secs. But when I comment the last line i.e.
//$collection->addFieldToFilter('type_id', 'configurable');
it takes less than 1.5 secs.
I have flat catalog enabled.
Question: Is there anything wrong with this collection?
I use Magento 1.6
Solution
Question: Is there anything wrong with this collection?
No.
If flat is enabled
I'm not sure, how magento checks which columns are generated into the flat table. Maybe the type_id is not and has to be joint into the flat table? Please check the query by $collection->load(true)
what magento is doing.
If flat is not enabled
This doesn't make sense to me. The column type_id
is a static attribute, means it is part of the base table catalog_product_entity
. It should be very fast to filter by this column, compared to other attributes.
The problem is, that the column has no index on it, so MySQL has to do a full table scan, but if you have no other filters on it, magento will get all products, so it does a full table scan either. What you can try is to add an index on the column if you need this query often or it is customer relevant.