I saw the numbers increasing in the mview_state table in the database and there is no cleanup.

Magento 2 mview_state

So I wonder what's the purpose of this table and the values that we have there?


mview_state is a table enabling your indexes to feed their index tables. That is the nutshell. Now, I have built a small dummy module with index this week.

the index file is as follows: (etc/indexer.xml)

   <?xml version="1.0" encoding="UTF-8"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
        <indexer id="cart_log" view_id="mbs_cart_log" class="Mbs\AdminScreen\Model\Indexer\CartLogCreator">
            <title translate="true">Cart Log Data</title>
            <description translate="true">Create Cart Log data</description>

there is a mview file (etc/mview.xml)

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
    <view id="mbs_cart_log" class="Mbs\AdminScreen\Model\Indexer\CartLogCreator" group="indexer">
            <table name="quote" entity_column="entity_id" />

The index is highly reliant on this mview file. The latter dictates that when a quote record is created, the entity_id is eventually stored in _cl table.

I will now describe where this _cl table comes from. In my module, my index table is mbs_cart_log. Magento will create a table called mbs_cart_log_cl as soon as your indexer is successfully registered. And this is the table that gets a record when the new quote record gets created. Also, an additional mview_state record will be created with the name of your view_id (the one in /etc/view/xml)

Once you have this working, then you just have to run the index you have just added, and you will see mview_state table being updated with a version id that matches the _cl table latest record.

Finally, but not least, your index table will contain the records that the indexer function executeFull will have extracted (mine is at \Mbs\AdminScreen\Model\Indexer\CartLogCreator::executeFull)

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.