Best WordPress SQL Queries to Make your WordPress Blog Faster

Most of the professional bloggers are using Self Hosted WordPress Blog because it’s user friendly and has support for Plugins which simplifies the task of users. However, more use of Plugin makes a WordPress Blog heavy.  Page Load time always plays a major role in the success or failure of any Website. Now a day, everyone wants to visit fast loading site. A recent survey shows that a slow loading site lose almost 45% of its visitors. That’s probably the reason why, Good bloggers invests money on Premium Products like MaxCDN, Good Hosts etc. If you can invest money for making your WordPress Blog faster, I will highly recommend you MaxCDN.

If you don’t want to use any Plugin or Premium Products like MaxCDN, there is another way to make your WordPress Blog faster i.e through SQL Queries. WordPress stores every single information in MySQL Database, be it Posts, Comments, Pages, Plugins, Meta information etc. Most of the new WordPress users are unaware of the fact that, useless contents like post revision, drafts, trash etc. increases the database and thus affects the page load time of your blog. By removing these tables from your Database, You can make your wordPress blog faster.  This can be easily done by using “WordPress SQL Queries” in phpMyAdmin. In this post, I will show you the same.

1. Delete wp_postmeta values which are not attached to any Post

Whenever you install or remove any plugins in your wordPress blog, they store some data in Post Meta table i.e wp_postmeta. These data still remains inside the same table even after removing Plugins which unnecessarily increases the size of wordPress database and thus affects the page load speed. Use the below given code to delete Post Meta values which are not attached to any post.

If you want to optimize the wp_postsmeta, use the below given SQL code. It will decrease the size of your database and will make your site much faster.

2. Bulk Delete all Spam comments

If your wordPress blog has lots of Spam Comments, you will find it difficult to delete all comments manually. However, this can be easily done through SQL queries. Just use the below given SQL query and delete all spam comments at once.

Sometime, Bloggers may want to delete all Pending comments at once. If you want to perform this operation, use the below given code.

3. Bulk delete all Trash comments

Just like Spam comments, Trash comments are also captures useless space in your wordPress database. You can free up that space by using below given query.

4. Bulk delete all Pending Posts in WordPress

When you allow registration on your wordPress blog, people may write different kind of posts. Without your approval, they won’t be published but they will still consume space in your database. If you want to get rid of this, Use the below given query.

5. Delete draft and auto-draft at once

An unpublished article in wordPress mostly goes into Draft category. Sometime, draft contains only useless posts which can increase your database size. To delete drafts and auto-draft posts at once, use the below given SQL Query.

6. Delete Revision 

When you edit any article in WordPress, it automatically creates many revision copies and thus increase burden on your wordPress database. When revision entries are less in numbers, You can neglect them. However, if you have large number of revisions, i will recommend you to delete them using below given query.


7. Delete all PingBack data

If your articles are popular, they will receive large number of Pingback. It also increases the size of your database which affect your page load speed. You can reduce the size of database by removing all such pingbacks. To do so, use the below given query.

How to use SQL Queries in WordPress?
As I mentioned earlier, WordPress stores all kind of data in MySql database. You can run all kind of SQL queries using phpMyAdmin. If you are new, Follow below given instructions:
  • Log into your cPanel account.
  • Scroll down until you find “Databases” section. In this section, Click on “phpMyAdmin“.

phpMyAdmin option in cPanel

  • Now you will see your Database on left hand side. If you are using only one WordPress site under your hosting account, You will only see one Database file on left side. If you are using more than one wordPress site under your hosting account, You will see more than one databases. To find the correct database, check your “wp.config” file.
  • If you already know the name of your database, Click on it. You would be able to see all tables. Just click on “SQL” tab as shown in below given figure:

Using SQL queries in WordPress


  • After clicking on “SQL” tab as shown in above figure, You will be redirected to another page where you will see a Query box. You can write your SQL queries in this box. Once done, click on “Go” button to execute the query. An example for the same is shown below:

Use sql queries for making wordPress faster


  • After clicking on “Go” button, you will get a confirmation message. Just click on ok and your are done. Let’s have a look on below given image:

Using sql queries in wordPress


  • That’s it. You are done. Use the above provided queries and fasten your wordPress blog without using any Plugin or Premium Tool.
From the Editor’s Desk
Plugins are the power of WordPress. All above mentioned tasks can also be done by using various Plugins but they makes your wordPress blog slower. That’s probably the reason why, most of the professioal bloggers suggest you to use only necessary plugins. If any task can be easily done without using plugins, there is no need to use them(I guess). If you are completely new to WordPress and want to use a free self hosted blog, Check this tutorial.
Using all above mentioned WordPress SQL Queries, you can improve the page load speed of your blog. Just use them and let me know, How it worked for you. If you have any other suggestion or Question, let me know.


  1. says

    Hi Ravi,

    WordPress gives us lots of choices. There are loads of plugins to choose from, just to accomplish a single task! Well, this is both a boon and bane…ha ha..

    As you said, using too many plugins is not recommended. At the beginning of my blogging days, I was so intrigued by plugins that I used more plugins than it was necessary!

    Okay, let’s talk about the excellent info you have provided here. Keeping database clean is a good way of boosting loading speed. There are plugins to get this job done. But, the codes you provided will get this job done, without having to add an extra plugin!

    So, more work, but less load! These codes are handy, particularly for those who have some knowledge in coding and all. But you’ve provided screenshots too. This makes using these codes easy for even newbie bloggers!

    Useful post bro! :)


  2. says

    Hi Ravi,

    Thanks for sharing with us this insight. I have of course being experiencing a longer loading time recently and I guess it will be boiling to the loads of spam comments I have being receiving lately.

    I am of course going to implement what you’ve recommended.

    • says

      Thanks for your comment Emmanuel. If your database is large, it will definitely affect your loading time. Using above mentioned queries, You can decrease its size and make your wordpress blog faster. I have already received lots of EMails where users has thanked me.

  3. says

    Hi Ravi,

    It’s good to have some insight knowledge about the database and tables used in WordPress. Thanks for the SQL commands to perform various tasks.

    At the same time, I’d like to know, why would anyone risk by accessing blog database directly when we already have plugins like ManageWP to do all these tasks from our WordPress Dashbaord? Do you think there is extra benefit using SQL method rather than plugin to manage all those tasks?

    • says

      Plugins are helpful but sometime, they won’t work. For example- When i published this post, a wordpress user asked me to check his blog because it was taking too much time to load. In his admin panel, he was using “Wp-Optimize” plugin as well. His database size was around 1 GB. Plugin failed to work properly there. Whenever i tried to use it, blog started showing “Maintenance Mode”. I tried 2-3 plugins but still failed to fix his issue. At the end, I performed sql queries and fixed his issue………………so Plugins are useful but not everytime…but SQL queries will work everytime……and yes, it doesn’t affect your page load time as well…..ha ha

  4. says

    It looks like a bit geeky article for me.  To be honest, as of now I don’t think I will be using it because my WordPress database is too small comparatively as my blog was newer one. However, this post may come handy for those who are troubling with their database space and wanted to speed up their blog

  5. says

    Wow….thanks a lot for sharing this. We should always keep a check on the load speed of our blogs. It has become very much important, and you’ve provided a few best WP SQL Queries to handle taht with care…..Thanks again…

    • says

      There is no risk in using these SQL queries via phpMyAdmin panel Chamal. If you allow other bloggers to post on your blog, don’t use one query which deletes “Pending Posts”. Other than that, You can use all queries without any issue.

  6. says

    This is a very good article that can help just about every WordPress site owner concerned about speed. Using sql queries are definitely easier than manually doing these one by one.

    But regarding the mass deleting all spam comments, it’s a good idea to first go through before doing that because there are usually a couple of genuine comments that get caught in the spam filters.

    Thanks to Piyush Mathur, for sharing this on I found it helpful and have “kingged” it.

  7. Sandeep says

    i just move my wordpress site to new host and some plugins are not working . Please Ravi can you give a solution to it ?

Leave a Reply

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

CommentLuv badge