Use multiple JOINs instead of CASE in _wp_meta_sql(). See #14572

git-svn-id: http://svn.automattic.com/wordpress/trunk@15580 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
scribu 2010-09-06 11:04:50 +00:00
parent 6fd94cd065
commit c27d704999
2 changed files with 32 additions and 22 deletions

View File

@ -4243,10 +4243,13 @@ function _wp_search_sql($string, $cols) {
* @since 3.1.0 * @since 3.1.0
* *
* @param array $queries An array of queries * @param array $queries An array of queries
* @param string $meta_id_column The column that holds the object id * @param string $primary_table
* @return string * @param string $primary_id_column
* @param string $meta_table
* @param string $meta_id_column
* @return array( $join_sql, $where_sql )
*/ */
function _wp_meta_sql( $queries, $meta_id_column ) { function _wp_meta_sql( $queries, $primary_table, $primary_id_column, $meta_table, $meta_id_column ) {
global $wpdb; global $wpdb;
$clauses = array(); $clauses = array();
@ -4262,29 +4265,39 @@ function _wp_meta_sql( $queries, $meta_id_column ) {
if ( empty( $meta_key ) ) if ( empty( $meta_key ) )
continue; continue;
$clause = $wpdb->prepare( "WHEN %s THEN meta_value ", $meta_key );
if ( empty( $meta_value ) ) { if ( empty( $meta_value ) ) {
$clauses[] = $clause . "IS NOT NULL"; $clauses[ $meta_key ] = "";
} elseif ( 'like' == $meta_compare ) { } elseif ( 'like' == $meta_compare ) {
$clauses[] = $clause . $wpdb->prepare( "LIKE %s", '%' . like_escape( $meta_value ) . '%' ); $clauses[ $meta_key ] = $wpdb->prepare( "LIKE %s", '%' . like_escape( $meta_value ) . '%' );
} else { } else {
$clauses[] = $clause . $wpdb->prepare( "$meta_compare %s", $meta_value ); $clauses[ $meta_key ] = $wpdb->prepare( "$meta_compare %s", $meta_value );
} }
} }
if ( empty( $clauses ) ) if ( empty( $clauses ) )
return ''; return array('', '');
return " $join = '';
AND CASE meta_key $where = '';
" . implode( "\n", $clauses ) . "
END $i = 0;
GROUP BY $meta_id_column foreach ( $clauses as $meta_key => $value_query ) {
HAVING COUNT(*) = " . count( $clauses ); $alias = $i ? 'mt' . $i : $meta_table;
$join .= "\nINNER JOIN $meta_table";
$join .= $i ? " AS $alias" : '';
$join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
$where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
if ( !empty( $value_query ) )
$where .= " AND $alias.meta_value $value_query";
$i++;
}
return array( $join, $where );
} }
/* /*
* Used internally to tidy up the search terms * Used internally to tidy up the search terms
* *

View File

@ -463,12 +463,9 @@ class WP_User_Query {
$meta_queries[] = wp_array_slice_assoc( $qv, array( 'meta_key', 'meta_value', 'meta_compare' ) ); $meta_queries[] = wp_array_slice_assoc( $qv, array( 'meta_key', 'meta_value', 'meta_compare' ) );
$meta_query_sql = _wp_meta_sql( $meta_queries, 'user_id' ); list( $meta_join, $meta_where ) = _wp_meta_sql( $meta_queries, $wpdb->users, 'ID', $wpdb->usermeta, 'user_id' );
$this->query_from .= $meta_join;
if ( !empty( $meta_query_sql ) ) { $this->query_where .= $meta_where;
$this->query_from .= " INNER JOIN $wpdb->usermeta ON ($wpdb->users.ID = $wpdb->usermeta.user_id)";
$this->query_where .= $meta_query_sql;
}
if ( !empty($qv['include']) ) { if ( !empty($qv['include']) ) {
$ids = implode( ',', wp_parse_id_list( $qv['include'] ) ); $ids = implode( ',', wp_parse_id_list( $qv['include'] ) );