Thursday 4 June 2015

Magento Re-index from shell which is super-useful for large data store

Reason of re-indexing from shell

Magento do provide the functionality of re-indexing in admin pane, but it might cause some timeout failure, which may brings a somewhat failure of re-indexing procedure, because of php execution limit and nginx/apache timeout. However, the shell script can avoid the timeout and execution limit, so it's better to re-index from shell for large data stores.

How to re-index from shell

Log on the website server and go into the Magento root directory.
Run the command of php shell/indexer.php help which will show the manual of using the script.

Here are some useful commands provided by the script

Get a list of indexers: php shell/indexer.php info
Get the status of current indexes: php shell/indexer.php --status
Re-index all: php shell/indexer.php reindexall #which is not recommended especially for large data website
Run one of the indexers: php shell/indexer.php --reindex indexer #indexer is coming from the list of indexers

Magento index modes

There are two Magento index modes. The first one is 'Update on save' which means the index will be rebuilt whenever the change related to the index has been made. While the other one is 'Manual Update' which makes the re-indexing executed only when the re-indexing command is ran manually or it's operated in the admin panel.
To conclude, it's highly recommended to set the mode to 'Manually update' for large data stores.

Set up Magento index modes

Set the mode to 'Update on save': php shell/indexer.php --mode-realtime indexer #indexer is from the index list
Set the mode to 'Manually save': php shell/indexer.php --mode-manual indexer #indexer is from the index list
After it's set to 'Manually save', the re-indexing command need to be added in the Linux cron table  to make rebuilding the index took place at a specific time which is supposed to be an idle time.
Note: the index mode should be set to 'Manually save' when doing any importation or it will be very slow for the importing process.

What if the re-indexing process is stuck for some reason

Restart nginx and php-fpm to stop the process. 
Go into the root directory of Magento and check the locks under the folder of var/locks/. And delete the locks if you want to stop the stuck process.
Go into Mysql and  check the table named index_process. And set the status from working to pending if you want to stop the stuck process.
Use indexer script to check the status of indexers to make sure there is no running indexers.
Run the indexer from shell instead of from the admin panel.

What are the statuses of indexers and their meanings

In Magento there are three statuses of indexers, which are quite confusing from the names,  comprising 'pending', 'working' and 'require reindex'.
'pending' means the index has been rebuilt and there is no need to re-index it.
'working' indicates the re-indexing process of the index is running.
'require reindex' shows the index is out of date and it needs to be rebuilt.


No comments:

Post a Comment