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

git-svn-id: http://core.svn.wordpress.org/trunk@22103 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ryan Boren 2012-10-03 12:35:06 +00:00
parent ca4275859f
commit 778ba1c1b2
1 changed files with 24 additions and 1 deletions

View File

@ -711,7 +711,30 @@ class WP_Meta_Query {
$join = array();
$where = array();
foreach ( $this->queries as $k => $q ) {
$key_only_queries = array();
$queries = array();
// Split out the meta_key only queries (we can only do this for OR)
if ( 'OR' == $this->relation ) {
foreach ( $this->queries as $k => $q ) {
if ( ! isset( $q['value'] ) && ! empty( $q['key'] ) )
$key_only_queries[$k] = $q;
else
$queries[$k] = $q;
}
} else {
$queries = $this->queries;
}
// Specify all the meta_key only queries in one go
if ( $key_only_queries ) {
$join[] = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column";
foreach ( $key_only_queries as $key => $q )
$where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) );
}
foreach ( $queries as $k => $q ) {
$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';