Over 2500 database queries per page load and over 10 second load times

I desperately need help figuring out why my site is making over 2500 database queries each time a page loads. The hosting company looked into it somewhat and said that it's accessing the usermeta table for each user on every page load, but couldn't tell me much more than that. I did have the permalinks set up as /%category%/%postname%/ and read somewhere that it can cause extra database queries so I switched it to /%postid%/%postname%/ just on the main site, but it hasn't made a difference. Also the site is using varnish cache so if your not logged each page is cached for 5 minutes and will appear to load fast for each subsequent visit, but it's still slow on first page loads and logged in users. If anyone can offer any insight into this I would greatly appreciate it. Please let me know if you need me to grant you access to the site if you would like to take a look. Thank you.

  • Michelle Shull

    Hey there, Sheridan, I think I can help.

    I took a look at the front end of your site. I think your issue might be coming from one of your BuddyPress plugins, there's a chance it could be outdated, so it's (essentially) running in circles and hitting your db.

    You also may want to increase the time between caches. Dynamic content (like comments, or frequently updated posts) needs a shorter cache life, but content that doesn't change can be cached for much longer. I usually give static content at least two weeks.

    If you could give me access, I can take a closer look and narrow this down for you.

  • Michelle Shull

    Hello! I took a peek, and I think I know what's hitting your database so hard, it's the BuddyStream. Since it's checking for any new posts, members, sites, comments, status updates, etc., all across your network, it's sending a bunch of queries to make sure all the info it wants to aggregate is current. The larger your network/membership list is, the harder it's going to have to work.

    You can work around this a couple of different ways.
    1. You can change the settings on BuddyPress so not as much information is collected for the Activity Stream.
    2. You can optimize the other parts of your site to free up resources for BuddyPress to use.

    To do that, I'd start with the basics.
    1. Make sure to stay on top of your theme, plugin, and WordPress updates.
    2. You've got a lot of inactive plugins, if you're not planning on reactivating them in the future, consider deleting them.

    And moving on to the intermediate stuff:
    1. Since you've got such a robust network, staying on top of database optimization can really help your site performance. You can do this through PHPMyAdmin (a program your host provides), if you're comfortable with databases, or you can manage it with a plugin. (There are several available for free from the WordPress plugin repository.)
    2. Another issue that can clog up your database and slow your performance is excessive post revisions. WP keeps all the post auto-saves it creates by default, so if you've got a lot of people writing lots of posts, you've probably got quite a collection of old revisions taking up space. This is another issue you can handle with some code, if you're comfortable, or with a plugin, like this one: https://wordpress.org/plugins/revision-control/

    Hope this helps! Have a great day!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.