index

Why need index

*Indexing is how Magento transforms data such as products and categories, to improve the performance of your storefront. For example, if you change the price of an item from $4.99 to $3.99. Magento must reindex the price change to display it on your storefront.

Without indexing, Magento would have to calculate the price of every product on the fly, taking into account shopping cart price rules, bundle pricing, discounts, tier pricing, etc. Loading the price for a product would take a long time, possibly resulting in cart abandonment.*

Add a custom indexer

create a config file app/code/VendorName/TestModule/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="custom_index" view_id="custom_index" class="VendorName/TestModule\Model\Indexer\ExampleIndexer" shared_index="custom_index">
        <title translate="true">Indexer Example</title>
        <description translate="true">Indexer Example</description>
    <dependencies>
            <indexer id="cataloginventory_stock"/>
        </dependencies>
    </indexer>
</config>

The id attribute is used to identify this indexer. You can call it when you want to check the status, mode or reindex this indexer by the command line.

The view_id is the id of view element, which will be defined in the mview configuration file.

The class attribute is the name to the class, which processes the indexer method.

The shared_index attribute is used for combining several indexers into groups. While reindexing one of the group indexers, the others will be labeled as valid.

The dependencies element is used to mark the dependencies between various indexers.

Create an app/code/VendorName/TestModule/etc/mview.xml config file, which will be responsible for the indexation by Cron.

<?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="custom_index" class="VendorName\TestModule\Model\Indexer\ExampleIndexer" group="indexer">
        <subscriptions>
            <table name="catalog_product_entity" entity_column="entity_id"/>
        </subscriptions>
    </view>
</config>

The class should implement the interfaces:
\Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface.
Where the \Magento\Framework\Indexer\ActionInterface interface contains the main indexation methods, and the \Magento\Framework\Mview\ActionInterface interface contains the methods that are applied for the Update By Schedule mode of an indexation.

<?php

namespace VendorName\TestModule\Model\Indexer;

class ExampleIndexer implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
{

    /**
     * Execute full indexation
     *
     * @return void
     */
    public function executeFull()
    {
        // TODO: Implement executeFull() method.
    }

    /**
     * Execute partial indexation by ID list
     *
     * @param int[] $ids
     *
     * @return void
     */
    public function executeList(array $ids)
    {
        // TODO: Implement executeList() method.
    }

    /**
     * Execute partial indexation by ID
     *
     * @param int $id
     *
     * @return void
     */
    public function executeRow($id)
    {
        // TODO: Implement executeRow() method.
    }

    /**
     * Execute materialization on ids entities
     *
     * @param int[] $ids
     *
     * @return void
     * @api
     */
    public function execute($ids)
    {
        // TODO: Implement execute() method.
    }
}

After we change our custom indexer to ‘update by schedule mode’, a mysql trigger will be created on table ‘catalog_product_entity’. When we change mode to ‘update on save’, the trigger will be deleted.

Practices Tests

References

Highly Recomend: COMPREHENSIVE GUIDE TO MAGENTO 2 INDEXING
Dev docs – Indexing overview
what is mview in magento2?
Digging Into Magento 2’s Partial Reindexing Implementation

发表评论

电子邮件地址不会被公开。 必填项已用*标注