Make WordPress Core

Changeset 55671


Ignore:
Timestamp:
04/21/2023 09:22:04 AM (12 months ago)
Author:
spacedmonkey
Message:

Taxonomy: Always lazily load term meta.

In [34529] introduced lazy loading of term meta. However, this was only in the context of WP_Query. Other parts of the codebase, like WP_Term_Query did not lazily load term meta. In this change, calls to update_termmeta_cache are now replaced with wp_lazyload_term_meta, that instead of priming term meta caches, just adds them to the queue to be primed it ever called. This results in far less database queries, as there a number of places where term meta is being primed unnecessarily and never used. Adding everything to the term meta queue, also means that if term meta is used, that is all loaded in a single database / cache call.

Props spacedmonkey, mukesh27, peterwilsoncc.
Fixes #57645.

Location:
trunk
Files:
10 edited

Legend:

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

    r55526 r55671  
    866866        if ( $args['update_term_meta_cache'] ) {
    867867            $term_ids = wp_list_pluck( $term_objects, 'term_id' );
    868             update_termmeta_cache( $term_ids );
     868            wp_lazyload_term_meta( $term_ids );
    869869        }
    870870
  • trunk/src/wp-includes/post.php

    r55642 r55671  
    76937693    }
    76947694
    7695     if ( $term_ids ) {
    7696         $lazyloader = wp_metadata_lazyloader();
    7697         $lazyloader->queue_objects( 'term', $term_ids );
    7698     }
     7695    wp_lazyload_term_meta( $term_ids );
    76997696}
    77007697
  • trunk/src/wp-includes/taxonomy.php

    r55526 r55671  
    14271427}
    14281428
     1429
     1430/**
     1431 * Queue term meta for lazy-loading.
     1432 *
     1433 * @since 6.3.0
     1434 *
     1435 * @param array $term_ids List of term IDs.
     1436 */
     1437function wp_lazyload_term_meta( array $term_ids ) {
     1438    if ( empty( $term_ids ) ) {
     1439        return;
     1440    }
     1441    $lazyloader = wp_metadata_lazyloader();
     1442    $lazyloader->queue_objects( 'term', $term_ids );
     1443}
     1444
    14291445/**
    14301446 * Gets all meta data, including meta IDs, for the given term ID.
     
    40044020 * @since 4.6.0
    40054021 * @since 6.1.0 This function is no longer marked as "private".
     4022 * @since 6.3.0 Use wp_lazyload_term_meta() for lazy-loading of term meta.
    40064023 *
    40074024 * @global wpdb $wpdb WordPress database abstraction object.
     
    40184035
    40194036        update_term_cache( $fresh_terms );
    4020 
    4021         if ( $update_meta_cache ) {
    4022             update_termmeta_cache( $non_cached_ids );
    4023         }
     4037    }
     4038
     4039    if ( $update_meta_cache ) {
     4040        wp_lazyload_term_meta( $term_ids );
    40244041    }
    40254042}
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r55657 r55671  
    196196        wp_set_current_user( 0 );
    197197
    198         $lazyloader = wp_metadata_lazyloader();
    199         $lazyloader->reset_queue( 'term' );
    200         $lazyloader->reset_queue( 'comment' );
     198        $this->reset_lazyload_queue();
    201199    }
    202200
     
    275273        }
    276274
     275    }
     276
     277    /**
     278     * Reset the lazy load meta queue.
     279     */
     280    protected function reset_lazyload_queue() {
     281        $lazyloader = wp_metadata_lazyloader();
     282        $lazyloader->reset_queue( 'term' );
     283        $lazyloader->reset_queue( 'comment' );
    277284    }
    278285
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r55591 r55671  
    305305
    306306        update_menu_item_cache( $query_result );
     307        get_term_meta( $term_id );
    307308
    308309        $args = $action->get_args();
     
    384385        $start_num_queries = get_num_queries();
    385386        wp_get_nav_menu_items( $this->menu_id );
     387        get_term_meta( $term_ids[0] );
    386388        $queries_made = get_num_queries() - $start_num_queries;
    387389        $this->assertSame( 6, $queries_made, 'Only does 6 database queries when running wp_get_nav_menu_items.' );
    388390
    389         $args = $action_terms->get_args();
    390         $last = end( $args );
    391         $this->assertSameSets( $term_ids, $last[1], '_prime_term_caches() was not executed.' );
     391        $args       = $action_terms->get_args();
     392        $first      = reset( $args );
     393        $term_ids[] = $this->menu_id;
     394        $this->assertSameSets( $term_ids, $first[1], '_prime_term_caches() was not executed.' );
    392395
    393396        $args = $action_posts->get_args();
  • trunk/tests/phpunit/tests/query/lazyLoadTermMeta.php

    r55608 r55671  
    4646     */
    4747    public function test_wp_queue_posts_for_term_meta_lazyload() {
     48        $this->reset_lazyload_queue();
    4849        $filter = new MockAction();
    4950        add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
  • trunk/tests/phpunit/tests/term/getTerms.php

    r54402 r55671  
    27572757     * @ticket 10142
    27582758     */
    2759     public function test_termmeta_cache_should_be_primed_by_default() {
     2759    public function test_termmeta_cache_should_be_lazy_loaded_by_default() {
    27602760        global $wpdb;
    27612761
     
    27802780        }
    27812781
    2782         $this->assertSame( $num_queries, $wpdb->num_queries );
     2782        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    27832783    }
    27842784
  • trunk/tests/phpunit/tests/term/meta.php

    r51568 r55671  
    117117        global $wpdb;
    118118
    119         // Clear any previous term IDs from the queue.
    120         wp_metadata_lazyloader()->reset_queue( 'term' );
    121 
    122119        $p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
    123120
     
    200197            add_term_meta( $t, 'foo', 'bar' );
    201198        }
    202 
     199        $this->reset_lazyload_queue();
    203200        $q = new WP_Query(
    204201            array(
  • trunk/tests/phpunit/tests/term/query.php

    r55083 r55671  
    4545
    4646        $this->assertSameSets( array( $term_2 ), $q->terms );
     47    }
     48
     49    /**
     50     * @ticket 57645
     51     */
     52    public function test_lazy_load_term_meta() {
     53        $filter = new MockAction();
     54        add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     55        register_taxonomy( 'wptests_tax_1', 'post' );
     56        register_taxonomy( 'wptests_tax_2', 'post' );
     57
     58        $term_1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
     59        $term_2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_2' ) );
     60
     61        $q = new WP_Term_Query(
     62            array(
     63                'taxonomy'   => 'wptests_tax_1',
     64                'fields'     => 'ids',
     65                'hide_empty' => false,
     66            )
     67        );
     68
     69        $this->assertSameSets( array( $term_1 ), $q->terms );
     70
     71        $q = new WP_Term_Query(
     72            array(
     73                'taxonomy'   => 'wptests_tax_2',
     74                'fields'     => 'ids',
     75                'hide_empty' => false,
     76            )
     77        );
     78
     79        $this->assertSameSets( array( $term_2 ), $q->terms );
     80
     81        get_term_meta( $term_1 );
     82
     83        $args     = $filter->get_args();
     84        $first    = reset( $args );
     85        $term_ids = end( $first );
     86        $this->assertSameSets( $term_ids, array( $term_1, $term_2 ) );
    4787    }
    4888
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r53942 r55671  
    616616     * @ticket 10142
    617617     */
    618     public function test_termmeta_cache_should_be_primed_by_default() {
     618    public function test_termmeta_cache_should_be_lazy_loaded_by_default() {
    619619        global $wpdb;
    620620
     
    636636        }
    637637
    638         $this->assertSame( $num_queries, $wpdb->num_queries );
     638        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    639639    }
    640640
     
    701701        }
    702702
    703         $this->assertSame( $num_queries, $wpdb->num_queries );
     703        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    704704    }
    705705
     
    734734        }
    735735
    736         $this->assertSame( $num_queries, $wpdb->num_queries );
     736        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    737737    }
    738738
Note: See TracChangeset for help on using the changeset viewer.