Magento maintenance script for IIS

Photo of author
Written By Jan Reilink

Windows Server systems administrator & enthusiast.

Important Magento maintenance, now for IIS too: optimize the speed and performance of your Magento ecommerce webshop by carrying out important maintenance. Remove old MySQL database log files and Magento cache data on a regular basis.

Magento eCommerce Maintenance: Improve Webshop Speed & Performance

Clear Magento cache and MySQL log tables, perform regular Magento maintenance on IIS web servers for better performance. Magento Community Edition is a very popular ecommerce and webshop solution. And very bloated as we all know. Anywhere you run your Magento webshop, it’s important to perform maintenance. Carrying out maintenance on a regular basis optimizes Magento performance.

Two of such important Magento maintenance tasks are clearing out and emptying the MySQL database cache and log tables (such as log_visitor, log_url), and Magento’s file system cache directories in /var. Most scripts and solutions out there are for Linux- and Unix webservers only. I decided to modify a Magento maintenance script, to run on Windows Server and IIS too.

To optimize MySQL databases, it utilizes my MySQLi multi_query statement to optimize all MySQL tables in one statement.

Ask your hosting provider to schedule this script as a Windows Server scheduled task, for instance once a day, and you’ll notice a speed improvement of your Magento webshop. Next, add support for WinCache and your Magento webshop is very, very fast. Even on IIS!

Magento. Photo credit: wmcclure333 via Flickr

Clear magento cache and log tables

Disclamer: this script is provided “AS-IS” and should not be put into production without testing! Save the following PHP code as maintenance.php and upload it to your web-root.

<?php if(isset($_GET['clean'])) { switch($_GET['clean']) { case 'log': clean_log_tables(); break; case 'var': clean_var_directory(); break; default: default_action(); break; } } else { default_action(); } function clean_log_tables() { $xml = simplexml_load_file('./app/etc/local.xml', NULL, LIBXML_NOCDATA); if(is_object($xml)) { $db['host'] = $xml->global->resources->default_setup->connection->host; $db['name'] = $xml->global->resources->default_setup->connection->dbname; $db['user'] = $xml->global->resources->default_setup->connection->username; $db['pass'] = $xml->global->resources->default_setup->connection->password; $db['pref'] = $xml->global->resources->db->table_prefix; $tables = array( 'aw_core_logger', 'dataflow_batch_export', 'dataflow_batch_import', 'log_customer', 'log_quote', 'log_summary', 'log_summary_type', 'log_url', 'log_url_info', 'log_visitor', 'log_visitor_info', 'log_visitor_online', 'index_event', 'report_event', 'report_viewed_product_index', 'report_compared_product_index', 'catalog_compare_item', 'catalogindex_aggregation', 'catalogindex_aggregation_tag', 'catalogindex_aggregation_to_tag' ); $mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['name']); if($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } foreach($tables as $table) { @mysqli->query('TRUNCATE `'.$db['pref'].$table.'`'); } $query = "SELECT CONCAT('OPTIMIZE TABLE ', GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';') INTO @optimizecmd FROM information_schema.tables WHERE table_schema=database(); PREPARE s1 FROM @optimizecmd; EXECUTE s1;"; $mysqli->multi_query($query); $mysqli->close(); } else { exit('Unable to load local.xml file'); } } function clean_var_directory() { $dirs = array( 'downloader/.cache/', 'downloader/pearlib/cache