Here is a Guru Killer Guide With the Excellent SQL Commands to Clean WordPress Database Which can Reduce the Database Size Up To 80% – Yes. All of us know almost everything, but there are things which can make you jump – read the guide on Indispensable MySQL queries for custom fields in WordPress.
At that time, that article was the most mentioned guide from this website, notably WordPress related almost all websites mentioned it and as a novice we got hacked via that particular page. We had no practical idea about SQL Injection. The article on MySQL Queries was like a Guru, but who did it was the Guru Killer. Today, there is not a single comment and ping back is present. One must be aware of the vulnerability of using Cache Plugins and WordPress comment form. Actually, Fetching Data from MySQL via PHP in WordPress is not very difficult.
Before we move towards coding part of this guide on SQL Commands to Clean WordPress Database, we must give you some links to increase your knowledge. On the lighter side, after many years; I wrote a blog my personal blog site – Life is MySQL Database Where One Can Not Run Queries.
---
SQL Commands to Clean WordPress Database : Know Something More Before You Clean Up !
We can not expect that a newbie will read this guide and apply them, it is per se quite dangerous. There is WordPress Plugin named WP DB Backup – it takes backup via WordPress Administration Panel – you must use it it to backup the database first. We have noticed, even if direct backup through PHPMyAdmin or Command Line Fails (yes!), this Plugin reliably work – only keep the settings to save the database on FTP.
We have shown how to do a Repairing Job of WordPress Database. It is not that, everyone is used with WordPress or may be not used with the basics – so, it is better to know what is SQL, what is MySQL, what is phpMyAdmin etc. Also know, there is noSQL Database like PostgreSQL. Yes, with modification, it is possible to run WordPress with PostgreSQL instead of MySQL – we do not want to go towards that topic.
For various reasons, usually due to our previously installed Plugins, Spams etc. our Database grows up and get filled up with non useful lines. Not only the size of database increases, it actually creates a big issue – it becomes very difficult to take a full backup. Many plugins, unfortunately mess-up the wp_postmeta Table.
SQL Commands to Clean WordPress Database
WP Optimize like plugins works for less for the result we want. As there are many users who has lesser idea about handling PHPMyAdmin; we will suggest to read this article – Remove WordPress Plugin From Database and watch the video embedded below.
wp_commentmeta Table
We already wrote how to reduce WP_commentmeta Table Database size in WordPress, there is also a PHP tweak for Akismet for solving the issue; this probably happens due to JSON based API issue, the database do not allow to delete what should be deleted, unfortunately the hacked version / modes version of PHP gets replaced when Akismet is updated. Not all part of Akismet is fully Open – the trick will work forever till WordPress changes their database type.
wp_postmeta Table
This is here :
1 2 3 4 | SELECT * FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL |
Not tested version (do not run without backup) :
1 2 3 4 5 | SELECT * FROM wp_postmeta WHERE NOT EXISTS ( SELECT * FROM wp_posts WHERE wp_postmeta.post_id = wp_posts.ID ); |
wp_posts Table
Probably you will never need it, still :
1 2 3 4 5 | SELECT * FROM {$wpdb->postmeta} as pm LEFT JOIN {$wpdb->posts} as p ON pm.post_id = p.ID WHERE p.ID IS NULL |
DO NOT RUN the above without knowing what you are going to do.
Others
These are more than enough, but if a certain plugin still have data in specific Table; this is the way to clean up :
1 2 | SELECT option_value FROM wp_options WHERE option_name = 'plugin_databasename' LIMIT 1 |
You must open the Table and select via GUI to clean the overhead. SQL command to optimize all the Tables :
1 | OPTIMIZE TABLE `wp_commentmeta`, `wp_comments`, `wp_links`, `wp_options`, `wp_postmeta`, `wp_posts`, `wp_terms`, `wp_term_relationships`, `wp_term_taxonomy`, `wp_usermeta`, `wp_users` |