The Magento indexing mechanism is one of the core functionalities that improve a site performance. However, when programming bulk imports of data it is ideal to stop the indexing until the import is completed. For two reasons, first the indexing will slow the process, as on each line of data imported a reindex of data will be performed. Secondly it can generate errors due to the amount of data that is constantly changing.
Switching off Indexes
The indexes can be switched off by disabling the reindexing on save option.
Given that the index process is referenced by the variable $process to disable the reindex on save the command is
$process->setMode(Mage_Index_Model_Process::MODE_MANUAL)->save();
Switching on Indexes
The indexes can be switched on by enabling the reindexing on save option.
Given that the index process is referenced by the variable $process to disable the reindex on save the command is
$process->setMode(Mage_Index_Model_Process::MODE_REAL_TIME)->save();
Reindex All
In Magento Admin site it is common to select all the processes and run a full reindex process. To replicate this process in programming terms, one needs to iterate over all index processes and execute the command reindexEverything().
$indexingProcesses = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexingProcesses as $process) {
$process->reindexEverything();
}
Running a specific index
Indexes can be loaded using either the process ID or the process code using the functions:
by ID:
$process = Mage::getSingleton('index/indexer')->getProcessById();
by CODE:
$process = Mage::getSingleton('index/indexer')->getProcessByCode(<process_code>);
The process code and ID can be found by checking the table index_process. The Magento provided indexes normally have the following ID and code.
Process Name | ID | Code |
Product Attributes | 1 | catalog_product_attribute |
Product Prices | 2 | catalog_product_price |
Catalog URL Rewrites | 3 | catalog_url |
Product Flat Data | 4 | catalog_product_flat |
Category Flat Data | 5 | catalog_category_flat |
Category Products | 6 | catalog_category_product |
Catalog Search Index | 7 | catalogsearch_stock |
Stock Status | 8 | cataloginventory_stock |
Tag Aggregation Data | 9 | tag_summary |
References:
- Magento reindex shell script — /shell/reindex.php
- Magento – Programmatically Disable Automatic Indexing