Magento 2 Cookbook
上QQ阅读APP看书,第一时间看更新

Managing Magento 2 indexes via the command line

In the current version of Magento, using indexes is one of the most important key features. Without the correct indexes, we will not be able to use Magento properly.

What do the indexes do, and why are they so important? One of the key elements is to make things run faster. Without indexing, Magento 2 would have to calculate data on the fly. In Magento 2, we will be using the following nine indexes:

  • Customer Grid: This indexer rebuilds the customer's grid. This is a new indexer in Magento 2 for optimized rendering of the adminhtml backend pages.
  • Category Products: This indexer creates the association between categories and products based on the associations that you set in the backend on the categories and relates to the Product Categories indexer. The flat catalog indexer creates a flat optimized table in the database.
  • Product Categories: This indexer creates the association between products and categories based on the associations that you set in the backend on the products and relates to the Category Products indexer. The flat product indexer creates a flat optimized table in the database.
  • Product Price: This indexer relates to the products and their advanced pricing options based on, for example, customer group, website, and catalog discount rules. The indexer aggregates the data in tables (catalog_product_index_price_*) and makes the selects (sorting and filtering) much easier.
  • Product EAV: This indexer reorganizes the Entity Attribute Value (EAV) product structure to the flat structure. The product EAV indexer is related to the Category Products and Product Categories indexer and creates a flat optimized table in the database.
  • Stock: This indexer rebuilds and calculates the current stock data.
  • Catalog Search: This indexer rebuilds the catalog product fulltext search.
  • Catalog Rule Product: This indexer creates and updates the created catalog price rule set.
  • Catalog Product Rule: This indexer creates and updates the created shopping cart price rule set.

The database table looks as follows:

The control panel of the backend looks like this:

Getting ready

For this recipe, we will use a Droplet created in Chapter 1, Installing Magento 2 on Apache and NGINX, at DigitalOcean, https://www.digitalocean.com/. We will be using NGINX, PHP-FPM, and a Composer-based setup. A working Magento 2 setup is required.

How to do it...

The following are the steps to implement the recipe:

  1. In Magento 2, we only have the option to change the Update on Save or Update by Schedule state:

    Update on Save: Index tables are updated immediately after the dictionary data is changed

    Update by Schedule: Index tables are updated by cron job according to the configured schedule

  2. We will be using the shell during this. This will be the only way to update your indexes. Go to your shell and run the following command from your web server directory:
    php bin/magento indexer:info
    

    We now get an overview of all the indexers.

  3. Now run php bin/magento indexer:status; this will give us an up-to-date status of the current indexes.
  4. Now run php bin/magento indexer:show-mode; this information is related to the Update on Save or Update by Schedule modes.
  5. We can switch the modes using the following command on the shell:
    php bin/magento indexer:info realtime customer_grid
    

    Using the mode option realtime (Update on Save) or schedule (Update by Schedule) may set the indexer.

  6. One of the most commonly used commands is as follows:
    php bin/magento indexer:reindex
    

    This will reindex all the indexers. However, you can also reindex them inpidually using the following command:

    php bin/magento indexer:reindex customer_grid
    

    We can also use the following command:

    php bin/magento indexer:reindex customer_grid catalog_category_product etc…
    
  7. Now you can rerun indexer:status to check whether all the indexers are up to date.

How it works…

Let's recap and find out what we did throughout this recipe. In steps 1 through 7, you learned how to use the bin/magento indexer.

In step 5, you learned how to check what the current status is of all the indexes. In step 4, we saw how to switch from the realtime Update on Save to the schedule Update on Schedule modes.

In step 6, you learned how to reindex them inpidually.

There's more…

You can also check the current status of the indexer using MySQL. Run the following command in the shell:

mysql -u <username> --database <dbname> -p -e "select * from indexer_state"