Support » Plugins » Hacks » Number of DB queries with a new WP_Query

  • Hi,
    I’m building (mostly with a self-teaching purpose) a simple plugin to query posts from another blog on a MS network via a shortcode (something like [network-posts blogs=”8″ layout=”grid” columns=”3″]), but I’m running into some performance issues/doubts.

    To my understading (and maybe I’m wrong), when creating and looping through a new WP_Query, only 1 new query should be executed on the database.

    But when I execute my shortcode (without switching to another blog), I see that that 7 more queries are added (I’m checking then with Query Monitor).

    If add a switch_to_blog() to the mix (by just switching to 1 blog, not looping through dozen of them), the additional queries become 32.

    Most of the additional queries are tied to these functions:
    update_meta_cache()
    WP_Post::get_instance()

    and when I use switch_to_blog(), each of these functions call a SELECT for each post that’s being loaded into the loop.

    So my question is if this is the expected behaviour, or is there a way to optimize it?

    The plugin is on github and this is the link of a simplified branch that I’m using for debugging purposes:
    https://github.com/langhenet/display-network-posts/tree/debug

    Here switch_to_blog() is commented in the php code, but you can activate it by uncommenting line 40 and 64.

    Thanks πŸ™‚

    Best

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, that’s what happens. You make one query, but WP makes several internally. A lot of it can’t be helped, but this is why we should strive to minimize queries. The effect of a single query we make multiplies when internal queries are factored in. A lot of times, these internal queries aren’t very “expensive” (in processor time), so the overall impact is minimal. Some of the more expensive queries are cached by WP, so the first request is very expensive, but subsequent related requests can be much more efficient.

    In some cases, especially for a query we know is going to be expensive, it’ll pay to write our own SQL query run through a $wpdb method to get exactly what we need without the extra internal queries. Of course, writing such queries is less “coder friendly” than using WP_Query arguments and in a lot of cases the difference in processor time doesn’t justify writing your own SQL query. But when it does, especially on extremely large DBs, the extra effort is well worth it.

    Thread Starter chickenrun

    (@chickenrun)

    Hi bcworks, thank you for our answer.

    So if I’ve understood correctly, you’re saiyng that:
    1) it’s normal that 1 new Wp_query adds ~7 new database queries
    2) I should think about using a custom query with the $wpdb method instead of just doing a switch_to_blog() standard WP_Query…

    am I right?

    thanks πŸ™‚
    best

    Moderator bcworkz

    (@bcworkz)

    If your only goal is to minimize queries, then yes. It’s an over-simplification though. Using the same logic, we should code everything in Assembly, or at least the original C. πŸ™‚ I wouldn’t worry about it too much unless that proved to be the bottleneck.

    Thread Starter chickenrun

    (@chickenrun)

    Hi Bcworkz, thanks for the clarification πŸ™‚

    As I wrote in the OP, I’m just trying to understand some basic logic of how wordpress works: I found myself with a quite large (in term of traffic) Multisite network to manage, and performance is definitely an issue πŸ™‚

    anyway, thanks again for your explanation

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Number of DB queries with a new WP_Query’ is closed to new replies.