WordPress.org

Make WordPress Core

Changeset 19918


Ignore:
Timestamp:
02/14/2012 03:09:35 PM (10 years ago)
Author:
ryan
Message:

Split the main WP_Query posts query into two queries to avoid temp tables. Leverage cache to avoid second query in persistent cache environments. Props scribu, cheald, prettyboymp. see #18536

Location:
trunk/wp-includes
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r19881 r19918  
    37043704        return $caller;
    37053705}
     3706
     3707/**
     3708 * Retrieve ids that are not already present in the cache
     3709 *
     3710 * @since 3.4.0
     3711 *
     3712 * @param array $object_ids ID list
     3713 * @param string $cache_key The cache bucket to check against
     3714 *
     3715 * @return array
     3716 */
     3717function _get_non_cached_ids( $object_ids, $cache_key ) {
     3718    $clean = array();
     3719    foreach ( $object_ids as $id ) {
     3720        $id = (int) $id;
     3721        if ( !wp_cache_get( $id, $cache_key ) ) {
     3722            $clean[] = $id;
     3723        }
     3724    }
     3725
     3726    return $clean;
     3727}
     3728
  • trunk/wp-includes/pluggable.php

    r19801 r19918  
    140140    global $wpdb;
    141141
    142     $clean = array();
    143     foreach ( $user_ids as $id ) {
    144         $id = (int) $id;
    145         if ( !wp_cache_get( $id, 'users' ) ) {
    146             $clean[] = $id;
    147         }
    148     }
     142    $clean = _get_non_cached_ids( $user_ids, 'users' );
    149143
    150144    if ( empty( $clean ) )
  • trunk/wp-includes/post-thumbnail-template.php

    r19750 r19918  
    6565
    6666    if ( ! empty ( $thumb_ids ) ) {
    67         get_posts( array(
    68                 'update_post_term_cache' => false,
    69                 'include' => $thumb_ids,
    70                 'post_type' => 'attachment',
    71                 'post_status' => 'inherit',
    72                 'nopaging' => true
    73         ) );
     67        _prime_post_caches( $thumb_ids, false, true );
    7468    }
    7569
  • trunk/wp-includes/post.php

    r19910 r19918  
    53195319    }
    53205320}
     5321
     5322/**
     5323 * Adds any posts from the given ids to the cache that do not already exist in cache
     5324 *
     5325 * @since 3.4.0
     5326 *
     5327 * @access private
     5328 *
     5329 * @param array $post_ids ID list
     5330 * @param bool $update_term_cache Whether to update the term cache. Default is true.
     5331 * @param bool $update_meta_cache Whether to update the meta cache. Default is true.
     5332 */
     5333function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) {
     5334    global $wpdb;
     5335
     5336    $non_cached_ids = _get_non_cached_ids( $ids, 'posts' );
     5337    if ( !empty( $non_cached_ids ) ) {
     5338        $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ",", $non_cached_ids ) ) );
     5339
     5340        update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
     5341    }
     5342}
     5343
  • trunk/wp-includes/query.php

    r19866 r19918  
    26072607            $found_rows = 'SQL_CALC_FOUND_ROWS';
    26082608
    2609         $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
    2610         if ( !$q['suppress_filters'] )
    2611             $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
     2609        $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
     2610
     2611        if ( !$q['suppress_filters'] ) {
     2612            $this->request = apply_filters( 'posts_request', $this->request, $this );
     2613        }
    26122614
    26132615        if ( 'ids' == $q['fields'] ) {
     
    26272629        }
    26282630
    2629         $this->posts = $wpdb->get_results($this->request);
     2631        if ( $old_request == $this->request && "$wpdb->posts.*" == $fields ) {
     2632            // First get the IDs and then fill in the objects
     2633
     2634            $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
     2635
     2636            $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
     2637
     2638            $ids = $wpdb->get_col( $this->request );
     2639
     2640            if ( $ids ) {
     2641                $this->set_found_posts( $q, $limits );
     2642
     2643                _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
     2644
     2645                $this->posts = array_map( 'get_post', $ids );
     2646            } else {
     2647                $this->found_posts = $this->max_num_pages = 0;
     2648                $this->posts = array();
     2649            }
     2650        } else {
     2651            $this->posts = $wpdb->get_results( $this->request );
     2652            $this->set_found_posts( $q, $limits );
     2653        }
    26302654
    26312655        // Raw results filter. Prior to status checks.
     
    26442668            $this->comments = $wpdb->get_results($comments_request);
    26452669            $this->comment_count = count($this->comments);
    2646         }
    2647 
    2648         if ( !$q['no_found_rows'] && !empty($limits) ) {
    2649             $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
    2650             $this->found_posts = $wpdb->get_var( $found_posts_query );
    2651             $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
    2652             $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
    26532670        }
    26542671
     
    27552772    }
    27562773
     2774    function set_found_posts( $q, $limits ) {
     2775        global $wpdb;
     2776
     2777        if ( $q['no_found_rows'] || empty( $limits ) )
     2778            return;
     2779
     2780        $this->found_posts = $wpdb->get_var( apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()', $this ) );
     2781        $this->found_posts = apply_filters( 'found_posts', $this->found_posts, $this );
     2782
     2783        $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
     2784    }
     2785
    27572786    /**
    27582787     * Set up the next post and iterate current post index.
Note: See TracChangeset for help on using the changeset viewer.