Make WordPress Core


Ignore:
Timestamp:
02/14/2012 03:09:35 PM (13 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.