Make WordPress Core

Changeset 53037


Ignore:
Timestamp:
03/31/2022 10:04:25 AM (5 years ago)
Author:
spacedmonkey
Message:

Taxonomy: Improve performance by taxonomy queries by adding a limitting requested terms.

Add a limit to the number of terms requested in taxonomy queries. This improves the performance of the query and the likelihood of hitting an existing term query cache.

Props Spacedmonkey, peterwilsoncc, flixos90.
Fixes #55360.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-tax-query.php

    r52455 r53037  
    638638                }
    639639
     640                if ( ! is_taxonomy_hierarchical( $query['taxonomy'] ) ) {
     641                        $args['number'] = count( $terms );
     642                }
     643
    640644                $term_query = new WP_Term_Query();
    641645                $term_list  = $term_query->query( $args );
  • trunk/tests/phpunit/tests/query/taxQuery.php

    r52577 r53037  
    16221622                $this->assertSameSets( array( $p ), $q->posts );
    16231623        }
     1624
     1625        /**
     1626         * @ticket 55360
     1627         *
     1628         * @covers WP_Tax_Query::transform_query
     1629         */
     1630        public function test_tax_terms_should_limit_query() {
     1631                $filter = new MockAction();
     1632                add_filter( 'terms_pre_query', array( $filter, 'filter' ), 10, 2 );
     1633                register_taxonomy( 'wptests_tax', 'post' );
     1634                $name = 'foobar';
     1635                $t    = self::factory()->term->create(
     1636                        array(
     1637                                'taxonomy' => 'wptests_tax',
     1638                                'name'     => $name,
     1639                        )
     1640                );
     1641
     1642                $p = self::factory()->post->create();
     1643                wp_set_object_terms( $p, array( $t ), 'wptests_tax' );
     1644
     1645                $q = new WP_Query(
     1646                        array(
     1647                                'fields'    => 'ids',
     1648                                'tax_query' => array(
     1649                                        array(
     1650                                                'taxonomy' => 'wptests_tax',
     1651                                                'field'    => 'name',
     1652                                                'terms'    => $name,
     1653                                        ),
     1654                                ),
     1655                        )
     1656                );
     1657
     1658                $filter_args = $filter->get_args();
     1659                $query       = $filter_args[1][1]->request;
     1660
     1661                $this->assertSameSets( array( $p ), $q->posts );
     1662                $this->assertStringContainsString( 'LIMIT 1', $query );
     1663        }
     1664
     1665        /**
     1666         * @ticket 55360
     1667         *
     1668         * @covers WP_Tax_Query::transform_query
     1669         */
     1670        public function test_tax_terms_should_limit_query_to_one() {
     1671                $filter = new MockAction();
     1672                add_filter( 'terms_pre_query', array( $filter, 'filter' ), 10, 2 );
     1673                register_taxonomy( 'wptests_tax', 'post' );
     1674                $name = 'foobar';
     1675                $t    = self::factory()->term->create(
     1676                        array(
     1677                                'taxonomy' => 'wptests_tax',
     1678                                'name'     => $name,
     1679                        )
     1680                );
     1681
     1682                $p = self::factory()->post->create();
     1683                wp_set_object_terms( $p, array( $t ), 'wptests_tax' );
     1684
     1685                $q = new WP_Query(
     1686                        array(
     1687                                'fields'    => 'ids',
     1688                                'tax_query' => array(
     1689                                        array(
     1690                                                'taxonomy' => 'wptests_tax',
     1691                                                'field'    => 'term_id',
     1692                                                'terms'    => array( $t, $t, $t ),
     1693                                        ),
     1694                                ),
     1695                        )
     1696                );
     1697
     1698                $filter_args = $filter->get_args();
     1699                $query       = $filter_args[1][1]->request;
     1700
     1701                $this->assertSameSets( array( $p ), $q->posts );
     1702                $this->assertStringContainsString( 'LIMIT 1', $query );
     1703        }
     1704
     1705        /**
     1706         * @ticket 55360
     1707         *
     1708         * @covers WP_Tax_Query::transform_query
     1709         */
     1710        public function test_hierarchical_taxonomies_do_not_limit_query() {
     1711                $filter = new MockAction();
     1712                add_filter( 'terms_pre_query', array( $filter, 'filter' ), 10, 2 );
     1713                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     1714                $name = 'foobar';
     1715                $t    = self::factory()->term->create(
     1716                        array(
     1717                                'taxonomy' => 'wptests_tax',
     1718                                'name'     => $name,
     1719                        )
     1720                );
     1721
     1722                $p = self::factory()->post->create();
     1723                wp_set_object_terms( $p, array( $t ), 'wptests_tax' );
     1724
     1725                $q = new WP_Query(
     1726                        array(
     1727                                'fields'    => 'ids',
     1728                                'tax_query' => array(
     1729                                        array(
     1730                                                'taxonomy' => 'wptests_tax',
     1731                                                'field'    => 'name',
     1732                                                'terms'    => $name,
     1733                                        ),
     1734                                ),
     1735                        )
     1736                );
     1737
     1738                $filter_args = $filter->get_args();
     1739                $query       = $filter_args[0][1]->request;
     1740
     1741                $this->assertSameSets( array( $p ), $q->posts );
     1742                $this->assertStringNotContainsString( 'LIMIT 1', $query );
     1743        }
    16241744}
Note: See TracChangeset for help on using the changeset viewer.