How to Optimize WordPress Database through Code And Plugin

The speed of a website has always been a top priority. And to optimize WordPress database has a primary connection to the overall quick performance of a website.

Therefore, taking care of your site’s database and regularly cleaning it up can do wonders for your digital experience.

In this article, we are going to share our best experience with you when it comes to the question of how to optimize WordPress database through code and plugin.

A database is a well structured storage where all your site data are located, from important files and folders to unnecessary spams, comments and even already deleted plugins and extensions.

Hence, if you are constantly configuring, changing, refreshing and playing with your website, extending its functionality or adding extra tools to boost its performance online.

You’d definitely need to optimize WordPress database through one of the practical means in order to avoid heavy loading time, cluttered operation and tons of time for your WP website to retrieve the needed information from a huge database.

Let’s see what can be done to significantly affect the overall state of your site and how to optimize WordPress database through code and plugin.

Optimize WordPress Database

Reminder! Before making any changes relating to database, make sure you have properly backed up your site and have its reserved copy in a safe place.

1. How to optimize WordPress database through code?

Use all the advantages of boosting the effectiveness of your database and quicker loading of your site’s pages with the optimization of WP database through code.

If you check WordPress Codex for understanding the structure of WP database, you will find that it consists of 12 core tables such as wp_commentmeta, wp_comments, wp_links, wp_posts, wp_terms, wp_users, etc.

But if you check your own database, you may find more than 12 tables, meaning that additional ones have been created manually, or by plugins/themes you have used.

Now, what you need to do in order to optimize WordPress database through code is to run SQL command “Optimize table”.

For example, let’s optimize wp_links table. In this case, you will need to run the following SQL query in your phpMyAdmin area.

Optimize table ‘wp_links’
You need to check tables having overhead by clicking “Check all” box from the dropdown menu, then choose “Optimize table” from Table maintenance and press “Go” button.

This process should accurately fight with overhead compiled in your database over the time. As soon as the optimization process is finished, you will receive a confirmation about it.

If you want to delete comments waiting for approval or other spam comments, you can do that by following this SQL command:

DELETE FROM wp_comments WHERE comment_approved = ‘spam’

To deal with the trash folder, where all types of deleted items are stored, can be done in the following way.

Add define (‘SAVEQUERIES’, true ) to wp-config.php.

Further, add the below mentioned code in the footer of the theme:

if ( current_user_can( ‘administrator’ ) ) {
   global $wpdb;
   echo “<pre>”;
   print_r( $wpdb->queries );
   echo “</pre>”;

By default, trash items are deleted after 30 days, however you can change this timespan and define it as you wish, for example, by putting the following piece of code to wp-config.php file, if you need to set the days as 10:
define( ‘EMPTY_TRASH_DAYS’, 10 ); // 10 days

2. How to optimize WordPress database through plugin?
As alternative to having to do with little coding, there are several database optimization WP plugins you can choose to have in neighborhood and considerably contribute to the clutter free and speedy performance of your website.

WP Optimize:
WP Optimize

download button

One of the best ways to optimize WordPress database through plugin has to do with WP Optimize.

This plugin enables you to perform an automatic cleaning of your website’s database meanwhile saving your efforts and financial means, as it is easy in usage and free to install.

You can organize the cleanup of your site periodically or set a schedule and configure WP Optimize to manage it according to the schedule, for example, weekly or twice a week, etc.

It will remove needless data, from spammed, unapproved comments and post revisions, to expired options, trackbacks and more.

You are also free to choose which needs to be optimized by selecting options on your Admin Dashboard, check Database statistics and savings, and finally, organize everything from any mobile or portable device as WP Optimize is also mobile friendly.

Advanced Database Cleaner:
advanced database cleaner

download button

If you want a faster and cleaner WordPress website with lightweight and competent database at its core, look no further than Advanced Database Cleaner.

Keep your database in the best mood and function with this plugin and give the desired improvement to your site without having to do with coding or another complex procedure from the backend.

Both free and premium options are available for you to test in order to get rid of all kinds of unnecessary things stored in your database.

Free version comes with a diversity of database cleanup useful features like deletion of old revisions, drafts of pages and posts, spam, trash and pending comments, automatic scheduled clean up availability, data optimization and website speed improvement, table view and cleaning, etc.

Optimize Database after Deleting Revisions:
optimize database delete revisions

download button

Another wonderful tool for cleaning and optimizing WP databases comes to be known as Optimize Database after Deleting Revisions.

It boasts over 90.000 active downloads and a huge army of satisfied WordPress site owners who have tried and succeeded in making their profiles more qualified in terms of speed and performance.

Spend no money on the acquisition of one of the simplest database optimization plugins available in the official WP library. Instead, you will get access to as many practical features as are enough to keep your database well organized and lightweight.

Hence, this plugin is ideal for deleting revisions of posts and pages, spammed comments and unused tags, trashed posts/pages/comments, automatic optimization as per your need, etc.

About Sonnal S Sinha

Sonnal S SinhaSonnal S Sinha is a passionate writer as well as WordPress and WooCommerce rockstar who loves to share insights on various topics through his engaging blog posts. He run successful website design and digital marketing company. With 15+ years of experience in WordPress themes development, he strives to inform and inspire readers with his thought-provoking content. He helps thousands small and medium businesses and startups create a unique online presence. Follow Sonnal S Sinha for your regular dose of knowledge and inspiration.