This article examines the various ways to maintain an efficient Magento database of any size.
Magento does many things well, but maintaining an efficient database is not one of them. Having many products is a good reason to have a large database. Sadly, this is not the only way your database can become large and sluggish. Maintaining the database through log cleaning can result in a dramatic improvement in site performance and latency. This guide explains how to optimize a large and potentially inefficient Magento database.
ATTENTION: Always backup your data before performing any operations on the database.
Magento maintains several tables for logging. These tables log things such as customer accesses and frequently-compared products. Magento has a mechanism for cleaning these logs regularly, but this feature is disabled by default and most customers do not enable it.
It is not unusual to see 2GB+ databases decrease their size by 75% after cleaning the logs. It is therefore critical to regularly perform this sort of maintenance, particularly if your time-to-first-byte latency begins increasing and you have already implemented the other performance tweaks.
You can can clean these logs by using one of the three following methods:
- Log cleaning in the Magento Admin
- log.php in the ../shell directory
- phpMyAdmin or MySQL client
The following tables are managed by Magento’s log cleaning function:
Log cleaning from the Admin Panel
- From the Magento Admin Panel, select System > Configuration.
- On the left, locate the Advanced menu and click System.
- From the System panel, click Log, and from the Enable Log Cleaning drop-down list, select Yes.
- In the Save Log Days field, enter 15 (see above).
- Click Save Config.
You can either configure the shell utility log.php as a cron job or run it manually.
- From the Magento root directory, type the command:php -f shell/log.php clean
- Use the –days switch to specify how many days of history to save.
Manual cleaning with phpMyAdmin
This is the most efficient way to clean the logs for those more comfortable working with databases. It is faster than the built-in Magento tools and it allows you to clean other tables not included in those tools. This procedure will consolidate the data inside those tables, often decreasing database size by as much as 95% and greatly reducing query times.
- Open the database in phpMyAdmin from the SiteWorx control panel.
- In the right frame, select the check box for the following tables:
- At the bottom of the page, from the With Selected list, and select Empty.
- A confirmation screen will appear. Click Yes. This will truncate all of the selected tables.
- Click the Structure tab at the top of the page.
- Select the same tables as you did in Step 2, then under the With Selected list, select Optimize.