How to optimize website database

Database can store a tremendous amount of information – all the posts, pages, comments, menus and any form of text data. The more developed site you have, the larger its database is. Along with it, the database may become 'cluttered' with the unnecessary items, like spam comments, drafts, closed or temporary files, etc.

Of course, it can result in your website poor speed performance, and even resource overusage of your cPanel account – a number of MySQL requests to a 'heavy' database may stuck in a queue hitting Entry Processes and CPU limits. Thus, a proper optimization is required to involve the efficiency with which data is retrieved from the database.

This guide will show you several ways to achieve this goal.

1. To begin with you need to locate the database assigned to your website. If you do not know its name, feel free to check it your website's configuration file:

CMS Path to the configuration file Line
WordPress installation root folder, wp-config.php define('DB_NAME', 'cpuser_database');
Joomla installation root folder, configuration.php public $db = 'cpuser_database';
PrestaShop installation root folder >>/config/settings.inc.php define('_DB_NAME_', cpuser_database');
OpenCart installation root folder, config.php define('DB_DATABASE', 'cpuser_database');
Drupal installation root folder >>/sites/default/settings.php 'database' => 'cpuser_database',
Moodle installation root folder,config.php $CFG->dbname,= 'cpuser_database';
Magento installation root folder >>/app/etc/local.xml <dbname><[! CDATA [cpuser_database] ]></dbname>
phpBB installation root folder, config.php $dbname = 'cpuser_database';


2. In our case we have a WordPress-based website, our database name is nctests_wp255:



3. Before making any changes, we strongly advise that you download the backup of your database. To do so, move to Files section > Backup menu:



4. Click on the database in question in Download a MySQL Database Backup menu to start the download:



5. Now you can proceed with the optimization itself. Navigate to Databases section> phpMyAdmin menu:



6. Click on the database in the left menu to expand the list of the tables:



7. Checkmark the needed table and select Optimize table from the drop-down menu as shown below:



8. In the same way you can optimize several tables at once, or all of them using Check All option:



9. As a result, you will get the following output:



10. Another way to get your database optimized is to remove the unnecessary data. It can be done via SQL command line with the following query:

DELETE FROM $table where <clause>;

where $table defines the name of the table that should be adjusted and <clause> defines the changes that should be performed:



11. Let's take a closer look at this option and consider the way it works.

Suppose, that you need to remove all the posts that contain some key word, the following command should be used:

DELETE FROM 'table'
WHERE 'column' like '%keyword%
'

NOTE: you need to replace table and column with the actual values of your database.

To do so, move to the table that contains posts of you website and find the corresponding column. In our case it is wp9x_posts and posts_title column:



Below you can see the final variant of our command:

DELETE FROM 'wp9x_posts'
WHERE 'post_title' like '%test%'




Once you hit Go, all the posts that have 'test' word in their title will be removed.

The output will look like:



12. If you wish to remove the posts for the specified period, feel free to use this one:

DELETE FROM 'table'
WHERE 'column' between 'datefrom' and 'dateto'


Again, table and column should be replaced with the actual values as well as datefrom and dateto.

NOTE: you need to specify the precise date and time which can be looked up in the database.



Our final command is:

DELETE FROM 'wp9x_posts'
WHERE 'post_date' between '2015-06-24 19:48:14' and '2016-07-20 23:27:23'


13. Suppose, that you need to remove the comments from a certain user. Here is the command to use:

DELETE FROM 'table'
WHERE 'column' = 'username'


which in our case is:

DELETE FROM 'wp9x_comments'
WHERE 'comment_author' = 'test_user'


or if you need to remove the posts with a certain status:

DELETE FROM 'table'
WHERE 'column' = 'status'


which in our case is:

DELETE FROM 'wp9x_posts'
WHERE 'post_status' = 'closed'


The same mode works for any other tables, columns and databases, you just need to set the corresponding values. You can also refer to MySQL documentation to learn more about possible manipulations and MySQL syntax.


That's it!

              
                      Need any help? Contact our HelpDesk

Updated
Viewed
48743 times

Need help? We're always here for you.

notmyip