Make WordPress Core


Ignore:
Timestamp:
10/15/2014 04:39:19 PM (12 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/query.php

    r29896 r29902  
    13771377        /**
    13781378         * @group taxonomy
     1379         * @ticket 18105
     1380         */
     1381        public function test_tax_query_single_query_multiple_queries_operator_not_in() {
     1382                $t1 = $this->factory->term->create( array(
     1383                        'taxonomy' => 'category',
     1384                        'slug' => 'foo',
     1385                        'name' => 'Foo',
     1386                ) );
     1387                $t2 = $this->factory->term->create( array(
     1388                        'taxonomy' => 'category',
     1389                        'slug' => 'bar',
     1390                        'name' => 'Bar',
     1391                ) );
     1392                $p1 = $this->factory->post->create();
     1393                $p2 = $this->factory->post->create();
     1394                $p3 = $this->factory->post->create();
     1395
     1396                wp_set_post_terms( $p1, $t1, 'category' );
     1397                wp_set_post_terms( $p2, $t2, 'category' );
     1398
     1399                $q = new WP_Query( array(
     1400                        'fields' => 'ids',
     1401                        'update_post_meta_cache' => false,
     1402                        'update_post_term_cache' => false,
     1403                        'tax_query' => array(
     1404                                'relation' => 'AND',
     1405                                array(
     1406                                        'taxonomy' => 'category',
     1407                                        'terms' => array( 'foo' ),
     1408                                        'field' => 'slug',
     1409                                        'operator' => 'NOT IN',
     1410                                ),
     1411                                array(
     1412                                        'taxonomy' => 'category',
     1413                                        'terms' => array( 'bar' ),
     1414                                        'field' => 'slug',
     1415                                        'operator' => 'NOT IN',
     1416                                ),
     1417                        ),
     1418                ) );
     1419
     1420                $this->assertEquals( array( $p3 ), $q->posts );
     1421        }
     1422
     1423        /**
     1424         * @group taxonomy
    13791425         */
    13801426        public function test_tax_query_single_query_multiple_terms_operator_and() {
Note: See TracChangeset for help on using the changeset viewer.