Your SlideShare is downloading. ×
0
Magento Indexers<br />Ivan Chepurnyi<br />Magento Trainer / Lead Developer<br />
Agenda<br />Magento Developers Meetup<br />Overview of Indexes Functionality<br />Creation of own indexes<br />
Let Imagine…<br />Magento Developers Meetup<br />… that Magento doesn’t have indexes:<br />The prices in product list are ...
It’s all about performance…<br />Magento Developers Meetup<br />The main goal is minimizing amount of operations to displa...
Definitions<br />Magento Developers Meetup<br />Indexed DataAggregated data for entity representation on the frontend list...
Index Workflow<br />Magento Developers Meetup<br />Event<br />Main Controller<br />Event<br />Events<br />Process<br />Man...
Event Types<br />Magento Developers Meetup<br />Save<br />	When indexed entity or related to it information was changed<br...
Observed Entities <br />Magento Developers Meetup<br />Indexed Entities<br />Product<br />Product Inventory<br />Category<...
Index Process<br />Magento Developers Meetup<br />Available Statuses<br />Pending<br />	Indicates that indexer is up to da...
Index Process<br />Magento Developers Meetup<br />Indexing Modes<br />Real-time<br />Manual<br />Update Index Data<br />Ev...
Indexer Flow <br />Magento Developers Meetup<br />Match Event<br />Main Controller<br />Index Process<br />Register Event ...
Mage_Index Module<br />Magento Developers Meetup<br />Main Controller<br />Mage_Index_Model_Indexer<br />Process<br />Mage...
Index Module<br />Indexers Modularity<br />Magento Developers Meetup<br />Mage_Index_Model_Indexer_Abstract<br />Catalog M...
Model<br />Mage_Index_Model_Indexer_Abstract<br />Indexer Structure<br />Magento Developers Meetup<br />Resource Model<br ...
What can you use?<br />Magento Developers Meetup<br />Mage_Index_Model_Indexer<br />getProcessByCode($indexerCode)<br />ge...
What you shouldn’t do…<br />Magento Developers Meetup<br />Invoke reindexAll method from index model/resource model, becau...
Creating own indexer<br />Magento Developers Meetup<br />Defining indexer in configuration<br />Designing index data table...
Featured Products<br />Magento Developers Meetup<br />There is easier way to create featured products functionality, but i...
Defining index in configuration<br />Magento Developers Meetup<br /><config><br /><!-- …. module configurtaions --><br /> ...
Designing index data table<br />Magento Developers Meetup<br />Adding new attribute to catalog product entity called is_fe...
Designing index data table<br />Magento Developers Meetup<br />$this->addAttribute('catalog_product', 'is_featured', array...
Designing index data table<br />Magento Developers Meetup<br />$table = new Varien_Db_Ddl_Table();<br />$table->setName($t...
Designing index data table<br />Magento Developers Meetup<br />$table = new Varien_Db_Ddl_Table();<br />$table->setName($t...
Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_In...
Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_In...
Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_In...
Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_In...
Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_In...
Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Ma...
Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Ma...
Implementing Resource Model<br />Magento Developers Meetup<br />if ($productId !== null) {<br />            if (!is_array(...
Implementing Resource Model<br />Magento Developers Meetup<br />$sqlStatement = $select->insertIgnoreFromSelect(<br />    ...
Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Ma...
Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Ma...
Applying Index for the frontend<br />Magento Developers Meetup<br />Observing and event catalog_product_collection_apply_l...
Liked it?<br />Magento Developers Meetup<br />Checkout our advanced <br />training programs:<br />http://www.ecomdev.org/m...
Questions?<br />
Upcoming SlideShare
Loading in...5
×

Magento Indexes

21,574

Published on

Presentation from Magento MeetUp on 17th June in Amsterdam

Published in: Technology
0 Comments
20 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
21,574
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
263
Comments
0
Likes
20
Embeds 0
No embeds

No notes for slide

Transcript of "Magento Indexes"

  1. 1. Magento Indexers<br />Ivan Chepurnyi<br />Magento Trainer / Lead Developer<br />
  2. 2. Agenda<br />Magento Developers Meetup<br />Overview of Indexes Functionality<br />Creation of own indexes<br />
  3. 3. Let Imagine…<br />Magento Developers Meetup<br />… that Magento doesn’t have indexes:<br />The prices in product list are calculated on the fly depending on catalog rules, tier prices for customer groups<br />Stock availability for configurable and bundle products can be calculated only after loading the product collection<br />Layered navigation data is build in real-time for product attributes information<br />Anchor categories recursively collects subcategories for filtering product list<br />
  4. 4. It’s all about performance…<br />Magento Developers Meetup<br />The main goal is minimizing amount of operations to display products to a customer<br />
  5. 5. Definitions<br />Magento Developers Meetup<br />Indexed DataAggregated data for entity representation on the frontend lists.<br />Indexer<br /> Generates index data on event or manual by process.<br />Index EventThe moment when entity or related to it information is changed and that affects its index data.<br />Index Process<br />Wrapper for indexer and contains information about its mode and status<br />Main Controller<br /> Forwards events to Index Process<br />
  6. 6. Index Workflow<br />Magento Developers Meetup<br />Event<br />Main Controller<br />Event<br />Events<br />Process<br />Manual<br />Invoke<br />Indexer<br />Indexed Data<br />
  7. 7. Event Types<br />Magento Developers Meetup<br />Save<br /> When indexed entity or related to it information was changed<br />Delete<br />When indexed entity or related to it one was deleted<br />Mass UpdateWhen batch of entities was updated. (Update Attributes on Product Grid)<br />
  8. 8. Observed Entities <br />Magento Developers Meetup<br />Indexed Entities<br />Product<br />Product Inventory<br />Category<br />Tag<br />Entities Scope<br />Customer Group<br />Website<br />Store Group<br />Store View<br />
  9. 9. Index Process<br />Magento Developers Meetup<br />Available Statuses<br />Pending<br /> Indicates that indexer is up to date<br />Running<br /> Index currently in process of full rebuilding index data.<br />Require ReindexStatus for notifying admin user, that index is not up to date and should be rebuild. <br />
  10. 10. Index Process<br />Magento Developers Meetup<br />Indexing Modes<br />Real-time<br />Manual<br />Update Index Data<br />Event<br />Event<br />Require Reindex<br />
  11. 11. Indexer Flow <br />Magento Developers Meetup<br />Match Event<br />Main Controller<br />Index Process<br />Register Event Data<br />Reindex Data<br />
  12. 12. Mage_Index Module<br />Magento Developers Meetup<br />Main Controller<br />Mage_Index_Model_Indexer<br />Process<br />Mage_Index_Model_Process<br />Indexer Base<br />Mage_Index_Model_Indexer_Abstract<br />
  13. 13. Index Module<br />Indexers Modularity<br />Magento Developers Meetup<br />Mage_Index_Model_Indexer_Abstract<br />Catalog Module<br />Mage_Catalog_Model_Product_Indexer_Eav<br />Mage_Catalog_Model_Product_Indexer_Flat<br />Mage_Catalog_Model_Product_Indexer_Price<br />Inventory Module<br />Mage_CatalogIndex_Model_Indexer_Stock<br />…<br />
  14. 14. Model<br />Mage_Index_Model_Indexer_Abstract<br />Indexer Structure<br />Magento Developers Meetup<br />Resource Model<br />Mage_Index_Model_Mysql4_Abstract<br />Matches event data and runs appropriate method in resource model for re-indexing<br />Works directly with database for generation of the indexed data. Usually all the data operated via MySQL queries.<br />
  15. 15. What can you use?<br />Magento Developers Meetup<br />Mage_Index_Model_Indexer<br />getProcessByCode($indexerCode)<br />getProcessCollection()<br />processEntityAction($entity, $entityType, $eventType)<br />Mage_Index_Model_Process<br />reindexAll()<br />reindexEverything()<br />setMode($mode)<br />
  16. 16. What you shouldn’t do…<br />Magento Developers Meetup<br />Invoke reindexAll method from index model/resource model, because it is better to let admin user know when the index was rebuild.<br />Process entity events directly with indexer, instead of passing data through the main controller. You never know which index may depend on this event.<br />
  17. 17. Creating own indexer<br />Magento Developers Meetup<br />Defining indexer in configuration<br />Designing index data table<br />Implementing model <br />Implementing resource model<br />Applying index on the frontend<br />
  18. 18. Featured Products<br />Magento Developers Meetup<br />There is easier way to create featured products functionality, but it is a simple example on what should be done for creation own indexer.<br />
  19. 19. Defining index in configuration<br />Magento Developers Meetup<br /><config><br /><!-- …. module configurtaions --><br /> <global><br /> <!-- …. module configurtaions --><br /> <index><br /><indexer><br /> <featured_products><br /> <model>your_module/indexer_featured</model><br /> </featured_products><br /> </indexer><br /> </index><br /> </global><br /></config><br />etc/config.xml<br />Indexer Code <br />Indexer Model <br />
  20. 20. Designing index data table<br />Magento Developers Meetup<br />Adding new attribute to catalog product entity called is_featured<br />Creating table that will contain product ids of products that are marked as featured products.<br />
  21. 21. Designing index data table<br />Magento Developers Meetup<br />$this->addAttribute('catalog_product', 'is_featured', array(<br /> 'type' => 'int',<br /> 'label' => 'Is featured',<br /> 'input' => 'select',<br /> 'source' => 'eav/entity_attribute_source_boolean',<br /> 'user_defined' => false,<br /> 'required' => false<br />));<br />Setup Script<br />Attribute Code <br />Yes/No Dropdown<br />
  22. 22. Designing index data table<br />Magento Developers Meetup<br />$table = new Varien_Db_Ddl_Table();<br />$table->setName($this->getTable(‘module/featured'));<br />$table->addColumn('product_id', Varien_Db_Ddl_Table::TYPE_INTEGER, 11, array(<br /> 'unsigned' => true,<br /> 'nullable' => false,<br /> 'primary' => true<br />));<br />$this->getConnection()->createTable($table);<br />Setup Script<br />Table Alias<br />Table Column<br />
  23. 23. Designing index data table<br />Magento Developers Meetup<br />$table = new Varien_Db_Ddl_Table();<br />$table->setName($this->getTable(‘module/featured'));<br />$table->addColumn('product_id', Varien_Db_Ddl_Table::TYPE_INTEGER, 11, array(<br /> 'unsigned' => true,<br /> 'nullable' => false,<br /> 'primary' => true<br />));<br />$this->getConnection()->createTable($table);<br />Setup Script<br />Table Alias<br />Table Column<br />
  24. 24. Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_Indexer_Abstract<br />{<br /> protected $_matchedEntities = array(<br />Mage_Catalog_Model_Product::ENTITY => array(<br />Mage_Index_Model_Event::TYPE_SAVE, <br />Mage_Index_Model_Event::TYPE_MASS_ACTION<br /> )<br />);<br />}<br />Defining Matching Events<br />Entity Type<br />Event Type<br />Event Types<br />
  25. 25. Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_Indexer_Abstract<br />{<br />// … other code<br />protected function _construct()<br /> {<br /> $this->_init(‘your_module/indexer_featured');<br /> }<br />}<br />Defining Indexer Resource Model<br />Resource model<br />
  26. 26. Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_Indexer_Abstract<br />{<br /> // … other code<br />public function getName()<br /> {<br /> return Mage::helper(‘your_module')->__('Featured Product');<br /> }<br /> public function getDescription()<br /> {<br /> return Mage::helper(‘‘your_module')->__('Indexes something');<br />}<br />}<br />Defining Indexer Information<br />Indexer Name in the admin<br />Indexer Description in the admin<br />
  27. 27. Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_Indexer_Abstract<br />{<br /> // … other code<br />protected function _registerEvent(Mage_Index_Model_Event $event)<br /> {<br /> /* @var $entity Mage_Catalog_Model_Product */<br /> $entity = $event->getDataObject();<br /> if ($entity->dataHasChangedFor('is_featured')) {<br /> $event->setData('product_id', $entity->getId());<br /> } elseif ($entity->getAttributesData()) {<br /> $attributeData = $entity->getAttributesData();<br /> if (isset($attributeData['is_featured'])) {<br /> $event->setData('product_ids', $entity->getProductIds());<br /> }<br />}<br />}<br />}<br />Register Event for Processing<br />Product Save Registering<br />Mass Action Registering<br />
  28. 28. Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_Indexer_Abstract<br />{<br /> // … other code<br /> protected function _processEvent(Mage_Index_Model_Event $event)<br /> {<br /> if ($event->getData('product_id') || $event->getData('product_ids')) {<br /> $this->callEventHandler($event);<br /> }<br />}<br />}<br />Processing Event<br />Calling processor in resource model<br />Entity Type<br />Event Type<br />catalogProductSave($event)<br />catalogProductMassAction($event)<br />
  29. 29. Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Mage_Index_Model_Mysql4_Abstract<br />{<br /> protected function _construct()<br /> {<br /> $this->_setResource(‘your_module');<br /> }<br />}<br />Define resource connection<br />Your module resource prefix<br />
  30. 30. Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Mage_Index_Model_Mysql4_Abstract<br />{<br /> // … other code<br /> protected function _reindexEntity($productId = null)<br /> {<br /> $select = $this->_getReadAdapter()->select();<br /> /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */<br /> $attribute = Mage::getSingleton('eav/config')<br /> ->getAttribute('catalog_product', 'is_featured');<br /> $select->from($attribute->getBackendTable(), 'entity_id')<br /> ->where('value = ?', 1)<br /> ->where('attribute_id = ?', $attribute->getId());<br />Indexing Method<br />Retrieving only featured product ids<br />
  31. 31. Implementing Resource Model<br />Magento Developers Meetup<br />if ($productId !== null) {<br /> if (!is_array($productId)) {<br /> $productId = array($productId);<br /> }<br /> $select->where('entity_id IN(?)', $productId);<br /> $this->_getWriteAdapter()->delete(<br /> $this->getTable(‘your_module/featured'),<br /> array(<br /> 'product_id IN(?)' => $productId<br /> )<br /> );<br /> } else {<br /> $this->_getWriteAdapter()->truncate($this->getTable(‘your_module/featured'));<br /> }<br />Indexing Method<br />If it is partial re-index, then delete only related indexed data<br />Otherwise clear all indexed data<br />
  32. 32. Implementing Resource Model<br />Magento Developers Meetup<br />$sqlStatement = $select->insertIgnoreFromSelect(<br /> $this->getTable(‘your_module/featured'),<br /> array('product_id')<br /> );<br /> $this->_getWriteAdapter()->query($sqlStatement);<br /> }<br />}<br />Fulfill index data from select we created before<br />Indexing Method<br />
  33. 33. Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Mage_Index_Model_Mysql4_Abstract<br />{<br /> // … other code<br /> public function reindexAll()<br /> {<br /> $this->_reindexEntity();<br /> }<br />}<br />Handling Events<br />Full index re-build<br />
  34. 34. Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Mage_Index_Model_Mysql4_Abstract<br />{<br /> // … other code<br />public function catalogProductSave($event)<br /> {<br /> $this->_reindexEntity($event->getData('product_id'));<br /> }<br />public function catalogProductMassAction($event)<br /> {<br /> $this->_reindexEntity($event->getData('product_ids'));<br /> }<br />}<br />Reindexing Events<br />Single Save Product Event<br />Mass Save Product Event<br />
  35. 35. Applying Index for the frontend<br />Magento Developers Meetup<br />Observing and event catalog_product_collection_apply_limitations_after<br />Joining index table to product collection select<br />Create sub-select filter for collection<br />
  36. 36. Liked it?<br />Magento Developers Meetup<br />Checkout our advanced <br />training programs:<br />http://www.ecomdev.org/magento-development-training-programs/advanced<br />Follow our blog posts:<br />http://www.ecomdev.org/blog<br />
  37. 37. Questions?<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×