WordPress database optimization – A complete tutorial 2017

When it comes to website performance, database optimization could be a huge challenge. The database stores almost all of the website content. For example your posts are there. This also applies for pages, comments, website settings, forms, links and so on. From that point on, it is easy to conclude that regular updates make the database larger which may affect the overall performance. This raises the question “How to optimize WordPress database and this way guarantee its best performance ?“.

Well, the answer is easy. The best way to maintain a well organised database is to remove the unnecessary elements. Here however, comes another question “How to identify the important elements and remove the rest ?”. In order to address this question, we will need first to understand how the database is being organized  and how it works.

WordPress database – what does it store ?

Understanding the inner working of the database is important, especially if its slow performance has already been identified as bottle neck and prevents speed improvements. By default WordPress has 11 tables, the number of which of course could be changed. Usually the reason for large number of tables are additionally installed plugins that are coded in a way to create new tables and store some data there. In this tutorial, we will be looking at the default structure and comment the best optimization practices that could be applied to it. Here is quick review of the default tables:

  • wp_commentmeta – here you can find information about the comments
  • wp_comments – this table stored the comments
  • wp_links – it contains links
  • wp_options – here you can find the general settings
  • wp_postmeta – as the name suggest, here you can find post meta information
  • wp_post – this tables contains posts, pages and CPT
  • wp_terms – your tags and categories can be found here
  • wp_terms_relationships – in simple terms, this table contains information about the relationships in your data
  • wp_term_taxonomy – here you can find more information about taxonomy used in wp_terms section.
  • wp_usermeta – this table holds user information
  • wp_users – here are the users

Basic WordPress database optimization

The basic approach here is based on simple interaction with PhpMyAdmin. This is most commonly used database management utility which supports optimize and repair options. Working on the database with this tool is recommended for users with little to none knowledge and experience. PhpMyAdmin is a well known tool that can be found in almost all web hosting control panels. There are a few simple steps you have to take:

  • Click PhpMyAdmin
  • Select your database from the left menu
  • Mark the tables you want to optimize or repair
  • Select the preferred option at the bottom and then click Go

If the database in question is quite large, let’s say over 500 MB, then taking backup is recommended. Based on my personal experience, I can say that this would work without any troubles with databases up to 100 MB. The following screen shot illustrates the whole interaction :

WordPress database optimization - A complete tutorial 2017

WordPress database optimization – A complete tutorial 2017

Another similar approach that would achieve the same goal is to enable the so called native optimization tool. To do so, you have to add the following line to wp-config.php file:

define('WP_ALLOW_REPAIR' , true);

When you are ready with this line and the file is saved, you can directly access the tool using this url:


This way WordPress will be forced to check and try to repair each one of the tables in your database. Note that depending on the server settings the execution may fail, so you will have to run it again.

Advanced WordPress database optimization techniques

The next chapter in this tutorial covers more advanced techniques that can either be done directly in the database or through wp-admin section. Let’s start with revisions.

Managing WordPress revisions

WordPress revision system was first introduced in version 2.6. Its primary goal is to keep copy of every draft and post of your blog or website. Unfortunately by default it does not have maximum number of copies, which means that it can significantly increase the database size. There are two simple options to overcome this potential issue. We can either manually limit the revisions or completely stop  them. Here is how to do it:

To limit WordPress revisions to 2, just add the following line to wp-config.php file and then save it:

define('WP_REVISIONS' , 2);

Another possible course of action is to completely disable this feature by adding this line to wp-config.php file:

define('WP_POST_REVISIONS', false);

Generally speaking disabling this fail save system is not recommended. If you accidentally close your browser and lose reliable internet connection, then you will not be able to retrieve your data. With that being said I would recommend keeping at least 2 copies. Another important point to note. Stoping revisions will take effect from now on, which means the old copies will not be removed. To remove them, you can either use plugin or delete the old entries using PhpMyAdmin.

Managing spam comments

It is true that nowadays a lot of blogs and websites suffer from spam. Applying a permanent solution could be hard task. Fortunately there are a few successful approaches. The first one is installing and activating Akismet. Another possible course of action is to disable completely the comments feature or manually review and either delete or  approve the newly posted ones. Depending on the website specifics and size, different approach might be applicable.

Deleted items in WordPress

WordPress has another fail save feature that helps to avoid data loss. Whenever you delete an item from the website, it goes first to trash and sits there until its complete deletion. This again has pros and cons, but disabling this feature is not recommended. You can automate the trash cleaning instead. To do so, you have to add the following line to wp-config.php file:

define( 'EMPTY_TRASH_DAYS', 2 );

Managing unused themes and plugins

As you may know some well known extensions create their own tables in the database and this way make it largest. In some cases, when you remove a particular extension its tables may remain in the database, so you will have to review it and remove the entries manually. This can easily be done using PhpMyAdmin. Of course this requires more advanced skill and knowledge. If you are new to the field, then generating a working backup would avoid a huge headache.

WordPress transients

Dealing with transient records is another important part of WordPress database optimization. In essence, transient records allow developers to store temporary records for some reason. It could be fail save purpose, some additional checks or verifications. With that being said, careful inspection would give a positive effect over the optimization. There are a number of useful plugins that would help you achieve good cleaning. The choice depends on your personal needs. Another approach is achieving the same using Mysql query. The following one is a good example:

$wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_%')" );

Do you want to share your opinion?

Your email address will not be published. Required fields are marked *


We are not industry specific. We are WordPress specific. We work with everyone to help them find solutions with their troubles.
If you show us the problem you are experiencing, we will show you how to fix it. It is that simple.

Reported WordPress issues

  1. Single Product page loses current class in parent menu page?
  2. Open Graph scraping www URL instead the URL it's given
  3. How to build for Single / Recurring events metabox for custom post type of events in wordpress?
  4. how to replace strings in the values of a particular custom field in all posts?
  5. a good and cheap server that you can change max execution time to 180 [on hold]
  6. jQuery scroll no run
  7. Wordpress template shown after migration to new web host
  8. wp_nav_menu not applying the right menu
  9. How can I dynamically call a Custom Value and insert it into a $ Variable? Current effort breaks website
  10. Posts Table with Search and Sort WordPress Plugin

WordPress problems we are working on

  1. Changing order of a list in FormidablePro with jQuery UI
  2. How can I insert / upload an external html file to wordpress?
  3. Wordpress add_post_meta with increment
  4. Display -webkit-flex not working in Ipad (chrome and safari)
  5. No products were found matching your selection
  6. How to remove and animation on menu for a Wordpress website using Divi theme
  7. How to insert an invideo ad script into an iframed video served from google drive
  8. How to start/play a wordpress acf oEmbed video with a seperate button?
  9. How to update a WordPress page from Java (org.springframework.web.client.RestClientException)?
  10. Best htaccess practises for Wordpress which compatible with Cloudflare and Flexible SSL?

Resolved issues

  1. upload media to wordpress rest api
  2. How to centre menu of Wordpress website without effecting mobile menu
  3. How can I call a value from a Custom Text Field and dynamically place it in Form Attribute?
  4. Center items and align text left on mobiles using flex?
  5. Reduce the gap or white space between the title and 'hr' line using HTML
  6. Wordpress Hide Add to Cart Multiple Categories
  7. What's wrong with this PHP code? It's for a WordPress forum plugin
  8. php array - groups of 3
  9. Wordpress redirect within plugin files
  10. Specific .gif getting corrupt in wordpress
wppotion - powered by persistence and passion