Magento Indexing

Magento uses indexes to improve the system’s performance. The indexes can be seen within the admin area under System -> Index Management.

Magento Indexing

So how do these indexes help improve performance? Let’s take the Flat Category and Flat Product indexes as examples. These indexes become visible in the Index Management section when the Enable Flat Catalog Category and Enable Flat Catalog Product settings are enabled in System -> Configuration -> Catalog.

Magento Indexing

These flat indexes transform the original EAV table structure for category and product data into a singular per-store flat table structure. Having the information in a single table makes selects faster.

So for example when on a category page when Magento loads the relevant product collection for that category, Magento does not have to query multiple tables to get the required information.

For categories all of its attributes are converted to table columns. For products, only the attributes that have their Used in product listing option set to Yes will be converted into table columns.

Magento uses the following naming conventions for flat tables.

  • catalog_category_flat_store_1
  • catalog_product_flat_1

Where 1 is the store ID.

Within the Index Management section, you have the ability to change the Index Mode of an index to Update on Save or Manual Update.

Magento Indexing

Update on Save means that information saved that is related to an index will keep the index tables up to date. Manual Update means that the tables aren’t kept up to date, and the indexes will need to be re-indexed manually, however performance may be gained when saving information in the admin area.

Indexes are configured in Magento’s config.xml files. The majority of the configuration resides in the config.xml file of the Mage_Catalog module.

<config>
    ....
    <global>
        ....
        <index>
            <indexer>
                <catalog_product_attribute>
                    <model>catalog/product_indexer_eav</model>
                </catalog_product_attribute>
                <catalog_product_price>
                    <model>catalog/product_indexer_price</model>
                </catalog_product_price>
                <catalog_url>
                    <model>catalog/indexer_url</model>
                </catalog_url>
                <catalog_product_flat>
                    <model>catalog/product_indexer_flat</model>
                </catalog_product_flat>
                <catalog_category_flat>
                    <model>catalog/category_indexer_flat</model>
                </catalog_category_flat>
                <catalog_category_product>
                    <model>catalog/category_indexer_product</model>
                </catalog_category_product>
            </indexer>
        </index>
        ....
    </global>
    ....
</config>

Therefore to define a custom indexer, we would need to specify the configuration in a custom module’s config.xml file.

<global>
    ......
    <index>
        <indexer>
            <some_key>
                <model>somemodule/custom</model>
            </some_key>
        </indexer>
    </index>
    ......
</global>

Then the model itself would need to extend the Mage_Index_Model_Indexer_Abstract class.

<?php
class Namespace_Somemodule_Model_Custom extends Mage_Index_Model_Indexer_Abstract {
    // Some stuff
}

Note: This article is based on Magento Community/Open Source version 1.9.