Search code examples
phpmysqlwordpressdatabase-performancebluehost

How to cache/speed Wordpress queries for logged in users?


Wordpress generates an awful lot of sql queries.

We used 'WP Fastest Cache' which creates static html pages from your platform and caches them. Howevr for logged in users, the static pages wont work.

Here is what I am seeing. 122 queries, 7.8s spent in db time! Moreover as you can see all the queries are pretty fast (~0.05s). Hence caching the queries is not going to help

enter image description here

I am using bluehost to deploy and mysql db.

Whats the best way to optimize this. Is there a way to run all queries in parallel on mysql or some other elegant solution?

I am thinking of building this on heroku using rails, but we dont have enough time currently so we have to figure out a way to optimize the db.


Solution

  • Moreover as you can see all the queries are pretty fast

    Those queries aren't so fast. 0.1s for a simple lookup on the options table is very slow.

    All of the queries in the image you posted are on the options table. I'm not sure if most of your queries are option queries or not, but if you want to reduce the total number of option queries, you can use the autoload parameter on update_option or add_option. From the documentation:

    $autoload
    (string) (optional) Should this option be automatically loaded by the function wp_load_alloptions() (puts options into object cache on each page load)? Valid values: yes or no.

    If you autoload your options they will all be fetched in a single query. This will reduce the total number of queries, but as a mentioned before, your queries shouldn't be taking that long to begin with.

    For existing options, you will have to delete them first and then re-add them using the autoload parameter:

    $val = get_option('some_option');
    delete_option('some_option');
    update_option('some_option', $val, true);
    

    By default, when you create options they are set to autoload so it is a bit odd that you have so many options that aren't autoloading.