If you own a WordPress website and have noticed that your site’s performance has become slow or you are experiencing loading issues, the cause may be the size of your database.
The WordPress database stores a large amount of information and over time, it can accumulate, negatively affecting your site’s performance.
To fix this issue, you can follow the steps below:
Limit post revisions: WordPress automatically saves revisions of your posts and pages in the database every time you edit them. Over time, these revisions can accumulate and increase the size of your database.
You can limit the number of revisions that are saved by using plugins or by adding custom code to your functions.php file to limit the amount of revisions that are stored.
define( 'WP_POST_REVISIONS', 3 );
Access the database: You can access your WordPress site’s database using a database management client like phpMyAdmin or through the command line if you have command line access to your server.
DELETE FROM wp_posts WHERE post_type = 'revision';
wp_postmeta table: The wp_postmeta table in the WordPress database stores metadata associated with posts on your website. This metadata can include information such as publication date, authorship, categories, tags, featured images, and other related information for each post. If your website has many posts or uses many plugins, the wp_postmeta table in your database may become too large.
This code will delete all metadata that references featured images that are not linked to any posts.
DELETE FROM wp_postmeta WHERE meta_key = '_thumbnail_id' AND post_id NOT IN (SELECT ID FROM wp_posts);
Deleted post metadata: When you delete a post in WordPress, the metadata associated with that post is retained in the wp_postmeta table. You can run the following MySQL command to delete all metadata associated with posts that no longer exist:
DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT ID FROM wp_posts);
Optimize wp_options table: You can optimize the wp_options table to reduce its size and improve the performance of your website. This can be done through phpMyAdmin or by running an SQL query to optimize the table.
OPTIMIZE TABLE wp_options;
Delete old transients: Transients are temporary options stored in the wp_options table that can accumulate over time and increase the size of the table. You can delete old transients by running SQL queries to delete expired transients:
DELETE FROM wp_options WHERE option_name LIKE ‘_transient_%’ AND option_value < NOW();
DELETE FROM wp_options WHERE option_name LIKE ‘_transient_timeout_%’ AND option_value < NOW();
Note: Please be aware that making changes to the database always carries the risk of causing issues on your website, so it’s important to backup your database and proceed with caution when following these steps.