Make WordPress Core

Changeset 22103


Ignore:
Timestamp:
10/03/2012 12:35:06 PM (12 years ago)
Author:
ryan
Message:

Improve performance of WP_Meta_Query when doing OR queries on meta keys. Props joehoyle, SergeyBiryukov. fixes #19729

File:
1 edited

Legend:

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

    r21185 r22103  
    712712        $where = array();
    713713
    714         foreach ( $this->queries as $k => $q ) {
     714        $key_only_queries = array();
     715        $queries = array();
     716
     717        // Split out the meta_key only queries (we can only do this for OR)
     718        if ( 'OR' == $this->relation ) {
     719            foreach ( $this->queries as $k => $q ) {
     720                if ( ! isset( $q['value'] ) && ! empty( $q['key'] ) )
     721                    $key_only_queries[$k] = $q;
     722                else
     723                    $queries[$k] = $q;
     724            }
     725        } else {
     726            $queries = $this->queries;
     727        }
     728
     729        // Specify all the meta_key only queries in one go
     730        if ( $key_only_queries ) {
     731            $join[]  = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column";
     732
     733            foreach ( $key_only_queries as $key => $q )
     734                $where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) );
     735        }
     736
     737        foreach ( $queries as $k => $q ) {
    715738            $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
    716739            $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
Note: See TracChangeset for help on using the changeset viewer.