Avoid redundant table joins in WP_Tax_Query.

IN clauses that are connected by OR require only a single table join. To avoid
extraneous joins, keep track of generated table aliases, and let sibling
clauses piggy-back on those aliases when possible.

Introduces WP_Tax_Query::sanitize_relation() to reduce some repeated code.

Adds unit tests to verify the JOIN consolidation, and integration tests for
cases where JOINS are being combined.

Props boonebgorges, otto42, jakub.tyrcha.
Fixes #18105.

git-svn-id: https://develop.svn.wordpress.org/trunk@29902 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2014-10-15 16:39:19 +00:00
parent c995866623
commit 56c82e534f
3 changed files with 284 additions and 13 deletions

View File

@@ -1374,6 +1374,52 @@ class Tests_Post_Query extends WP_UnitTestCase {
$this->assertEquals( array( $p3 ), $q->posts );
}
/**
* @group taxonomy
* @ticket 18105
*/
public function test_tax_query_single_query_multiple_queries_operator_not_in() {
$t1 = $this->factory->term->create( array(
'taxonomy' => 'category',
'slug' => 'foo',
'name' => 'Foo',
) );
$t2 = $this->factory->term->create( array(
'taxonomy' => 'category',
'slug' => 'bar',
'name' => 'Bar',
) );
$p1 = $this->factory->post->create();
$p2 = $this->factory->post->create();
$p3 = $this->factory->post->create();
wp_set_post_terms( $p1, $t1, 'category' );
wp_set_post_terms( $p2, $t2, 'category' );
$q = new WP_Query( array(
'fields' => 'ids',
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'terms' => array( 'foo' ),
'field' => 'slug',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'category',
'terms' => array( 'bar' ),
'field' => 'slug',
'operator' => 'NOT IN',
),
),
) );
$this->assertEquals( array( $p3 ), $q->posts );
}
/**
* @group taxonomy
*/

View File

@@ -218,4 +218,138 @@ class Tests_Tax_Query extends WP_UnitTestCase {
$this->assertTrue( is_wp_error( $tq->queries[0] ) );
}
/**
* @ticket 18105
*/
public function test_get_sql_relation_or_operator_in() {
register_taxonomy( 'wptests_tax', 'post' );
$t1 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t2 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t3 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$tq = new WP_Tax_Query( array(
'relation' => 'OR',
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t1,
),
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t2,
),
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t3,
),
) );
global $wpdb;
$sql = $tq->get_sql( $wpdb->posts, 'ID' );
// Only one JOIN is required with OR + IN.
$this->assertSame( 1, substr_count( $sql['join'], 'JOIN' ) );
_unregister_taxonomy( 'wptests_tax' );
}
/**
* @ticket 18105
*/
public function test_get_sql_relation_and_operator_in() {
register_taxonomy( 'wptests_tax', 'post' );
$t1 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t2 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t3 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$tq = new WP_Tax_Query( array(
'relation' => 'AND',
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t1,
),
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t2,
),
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t3,
),
) );
global $wpdb;
$sql = $tq->get_sql( $wpdb->posts, 'ID' );
$this->assertSame( 3, substr_count( $sql['join'], 'JOIN' ) );
_unregister_taxonomy( 'wptests_tax' );
}
/**
* @ticket 18105
*/
public function test_get_sql_nested_relation_or_operator_in() {
register_taxonomy( 'wptests_tax', 'post' );
$t1 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t2 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t3 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$tq = new WP_Tax_Query( array(
'relation' => 'OR',
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t1,
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t2,
),
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t3,
),
),
) );
global $wpdb;
$sql = $tq->get_sql( $wpdb->posts, 'ID' );
$this->assertSame( 2, substr_count( $sql['join'], 'JOIN' ) );
_unregister_taxonomy( 'wptests_tax' );
}
}