The MU forums have moved to WordPress.org

Overhead of switch_to_blog: is 10s of queries normal? (12 posts)

  1. rebootnow
    Member
    Posted 14 years ago #

    I have a WPMU installation where loading a blank page (i.e. no Loop) generates ~25 queries. The loop and the sidebar add another 10 or so queries.

    So ~35 in total.

    I wanted to pull some blog posts from another blog so I used...

    switch_to_blog( 2 ); 
    
    {secondary loop here} 
    
    restore_current_blog()

    The number of queries went through the roof. So I experimented with different situations and if I *only* switch to the second blog and restore the current blog (i.e. empty secondary loop) it generates another 25 queries.

    Is this normal? It seems to map to the original 25 queries, implying that restore_current_blog() repeats the setup of the first blog from scratch.

    If this is expected...
    1. Is there another way to do a secondary loop without "switch_to..." - "restore_current..."?
    2. Alternatively, is there a way to cache the first setup and not repeat those 25 queries?

    Any insight would be much appreciated. My DB is groaning.

  2. andrea_r
    Moderator
    Posted 14 years ago #

    That's why there's alternatives to pulling posts. Switch to blog gets expensive, as you can see.

    What were you trying to do?

  3. rebootnow
    Member
    Posted 14 years ago #

    Thanks Andrea.

    I think the specifics illustrate best. Under the first post on http://youlookfab.com you will see a section of the page with the heading "Angie's latest additions to the store...". These items are pulled from a separate blog.

    Before The Loop starts I switch to this other blog and get the 5 most recent items.

  4. andrea_r
    Moderator
    Posted 14 years ago #

    hang on, Brajesh did this a smoother way, just gotta go find it.

    Could also parse the feed from the other blog, but I'm not a fan of pulling internal feeds back & forth across the site either.

  5. andrea_r
    Moderator
    Posted 14 years ago #

    AHA! Found it. :)

    See this thread:
    http://buddypress.org/forums/topic/blog-description-from-a-specific-blog#post-31363

    Basically, here's the code:

    <?php
    $blog_id=7;//or what ever
    $description=get_blog_option($blog_id,"blogdescription");
    echo $description;//show the description
    ?>

    In this case it gets the description, but you can pull whatever you like really.

  6. rebootnow
    Member
    Posted 14 years ago #

    Awesome. Thanks very much Andrea. Looks like just what the doctor ordered. I'll give it a shot tonight.

  7. rebootnow
    Member
    Posted 14 years ago #

    It seems that get_blog_option() can only get information from the wp_?_options table. But I looked at http://codex.wordpress.org/WPMU_Functions and there is another function, get_blog_post(), that will return a post object if you provide the blog ID and the post ID.

    Getting there...

  8. rebootnow
    Member
    Posted 14 years ago #

    Nope, the exact helper function I need doesn't exist.

    I think I'll just get the data by coding the exact SQL queries I need. This is probably what I should have done in the first place.

    BTW, a lot of the WPMU helper functions actually use switch_to_blog() / restore_current_blog(), so they have large overhead. One example of this is the get_blog_permalink() helper, which by my calculations might involve as many as 50 queries. That's quite a price to pay for 1 permalink.

  9. tdjcbe
    Member
    Posted 14 years ago #

    Haven't looked into this a great deal but wouldn't using a file cache like wp-super-cache help in this case? Pull it once and have it saved as a file for next time?

  10. rebootnow
    Member
    Posted 14 years ago #

    wp-super-cache solves the problem for users who aren't logged in.

    For users that are logged in the page is generated dynamically and the standard object caching mechanism might help. But I experimented with this and it doesn't seem to make a big difference to have caching enabled. Perhaps because switch_to_blog() re-initializes the cache.

    The other thing is that I can't use wp-super-cache right now because it is incompatible with one of my own plugins.

    Some direct db queries to get the entries I need now look simple by comparison. :)

  11. r-a-y
    Member
    Posted 13 years ago #

    Just replying to this issue as I was interested myself.

    I wanted to use a loop from a sub-blog on the homepage.

    Using a loop with switch_to_blog($blog_id) / restore_current_blog() resulted in 25 extra queries.

    Using a loop with $wpdb->set_blog_id($blog_id) / $wpdb->set_blog_id($orig_blog_id) resulted in 14 queries. Although if you use this method, you have to manually recode the post permalinks.

    So you save about 11 queries if you go the set_blog_id() route.
    Just an FYI.

    Might just use a cached RSS feed.

  12. andrea_r
    Moderator
    Posted 13 years ago #

    Yep.

    Ron & Donncha knocked around some ideas to make it smoother, didn't get very far.

About this Topic

  • Started 14 years ago by rebootnow
  • Latest reply from andrea_r