REGISTER NOW
Monday • 08.08.22 • 07:40
3 minutes for reading
Dmitry Rybkin
Magento 2 Backend Developer | IT Delight
Back-EndJavaScriptMagento

Magento 2 indexes

What are indexes in Magento?

Indexes are a way to transform and optimize data to improve site performance. Data such as product prices, information from catalogs, user details, is not efficient to receive or calculate dynamically every time.

Magento indexes this data into specialized tables and retrieves it upon request,updating the associated indexes whenever the corresponding.

Definitions:

Dictionary is the original data that will be further indexed.
Index – a representation of already optimized and ready-to-read data from the dictionary.
Indexer – an object that creates an index.

Operating modes for the indexing process

Update on Save – indexes tables immediately after changing the data in the dictionary.
Update by Schedule – indexes tables by cron schedule.

Application of indexes

Consider a simple example and create your own Magento indexer.

Magento development often requires the creation of custom rules, which may be used to alter front-end features such as product tags. These rules can be quite complex, depending on the user group, price range, date, etc. Calculating this “on the fly” is quite expensive and not optimal, so we will place our set of rules in a conditional table “product_label_rules” and write an indexer for it.

Let’s create our indexer class.

<?php

namespace Learn\Index\Model\Indexer;

/**
 * Class ProductLabelsIndexer
 * @package Learn\Index\Model\Indexer
 */
class ProductLabelsIndexer
    implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
{

    public function executeFull()
    {
        // TODO: Implement executeFull() method.
    }

    public function executeList(array $ids)
    {
        // TODO: Implement executeList() method.
    }

    public function executeRow($id)
    {
        // TODO: Implement executeRow() method.
    }

    public function execute($ids)
    {
        // TODO: Implement execute() method.
    }
}

There are two types of reindexing: full (Full reindex) and partial (Partial reindex).
Full rebuilds all indexed tables, partial rebuilds only those indexed tables that have changed.

Consider what each class method is responsible for:
executeFull – works with all data from the index, called when the reindex is executed by the command from the bin/magento indexer:reindex console
execute – used by mview, allows you to process data when updating in the “Update on schedule” mode
executeList – works with a set of changed entities from the database
executeRow – works with a single entity using a plugin

Define the newly created indexer in the config file – etc/indexer.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
<indexer id="product_labels_indexer" view_id="product_labels_indexer" class="Learn\Index\Model\Indexer\ProductLabelsIndexer">
<title translate="true">Product Labels Indexer</title>
<description translate="true">Custom index</description>
</indexer>
</config>

id – unique indexer identifier
view_id – identifier in the etc/mview.xml file
class — implementing class
title – indexer title
description — description of the indexer

The mview.xml config is responsible for tracking changes in the database for a specific entity, in our case this is a table with rules for products

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
<view id="product_labels_indexer" class="Learn\Index\Model\Indexer\ProductLabelsIndexer" group="indexer">
<subscriptions>
<table name="product_label_rules" entity_column="entity_id" />
</subscriptions>
</view>
</config>

id – unique indexer identifier
view_id – identifier in the etc/mview.xml file
class — implementing class
title – indexer title
description — description of the indexer

The mview.xml config is responsible for tracking changes in the database for a specific entity, in our case this is a table with rules for products

Using the bin/magento indexer:info command, you can verify that the new indexer has appeared in the Magento indexer list.

The bin/magento indexer:reindex [indexer_name] command starts the reindex process for all existing indexes. By specifying the indexer name as a parameter, you can start one specific reindex.

Creating your own indexers in Magento is quite a useful feature and can help you avoid unnecessary data calculations every time they are requested. What’s more, it will have a positive effect on website optimization.

Back

Best authors

Sidovolosyi Bogdan
Dmitry Rybkin
Admin Pro Magento
Alexander Galich and Stanislav Matyavin
Alexander Galich
Yevhenii Trishyn
Abramchenko Anton
Sidovolosyi Bohdan
Shuklin Alexander

Similar topics

  • Advanced JS bundling
  • Backend
  • e-commerce
  • graphics
  • Hyvä
  • Hyvä compatible module
  • Hyvä compatible modules for Magento 2
  • Hyvä Styles
  • indexes
  • Integration Tests
  • Jquery Compat
  • JS
  • JS loading
  • loaded JS files
  • Magento 2
  • Magento 2 indexes
  • Magento 2 theme
  • Magento Functional Testing Framework
  • Magento JS bundling
  • Magento JS merge
  • Magento layouts
  • Magento Templates
  • Magento2
  • MagePack bundling
  • Message Broker
  • MySql
  • optimization
  • PhpStorm
  • Pricing
  • Pricing types
  • product prices
  • proxy classes
  • RabbitMQ
  • Redis
  • Redis \
  • Reduce JS size
  • Tailwind CSS
  • Testing Framework
  • Year in Review
  • Оptimization