Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/09/2016 03:00:13 PM (7 years ago)
Author:
boonebgorges
Message:

Groups: Overhaul BP_Groups_Group::get() SQL query.

The new query follows the WP and BP convention of "split" queries: one
for the IDs matching the query parameters, and a second one for the
objects corresponding to the matched IDs. This configuration allows for
improved caching and better performance, especially on installations
with large numbers of groups.

The rewrite serves as a convenient excuse to address a number of
longtime annoyances with the way group queries work:

  • Previously, comma syntax was used for table joins, rather than the JOIN keyword. This required some string manipulation when using external tools for generating SQL fragments, such as WP_Tax_Query and WP_Meta_Query. See #5099, #5874. We now use the more standard JOIN syntax.
  • The logic for assembling the "total" query is overwhelmingly simpler.
  • Previously, group queries required three joined tables: the groups table, one groupmeta table (for last_activity), and a second groupmeta table (for total_member_count). After this changeset, these tables will only be joined when needed for sorting (orderby or type). The last_activity and total_member_count properties, when needed for display in a group loop, are lazyloaded from groupmeta - where they're precached by default (see update_meta_cache) - rather than pulled as part of the primary query, and are made available via __get() for backward compatibility.

See #5451, #5874. Fixes #5099.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r10937 r11071  
    397397    /**
    398398     * @group get
     399     */
     400    public function test_get_with_type_alphabetical() {
     401        $time = time();
     402        $g1 = $this->factory->group->create( array(
     403            'name' => 'A Group',
     404            'date_created' => bp_core_current_time(),
     405        ) );
     406        $g2 = $this->factory->group->create( array(
     407            'name' => 'D Group',
     408            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
     409        ) );
     410        $g3 = $this->factory->group->create( array(
     411            'name' => 'B Group',
     412            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100000 ),
     413        ) );
     414        $g4 = $this->factory->group->create( array(
     415            'name' => 'C Group',
     416            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 1000 ),
     417        ) );
     418
     419        $groups = BP_Groups_Group::get( array( 'type' => 'alphabetical' ) );
     420        $found = wp_parse_id_list( wp_list_pluck( $groups['groups'], 'id' ) );
     421        $this->assertEquals( array( $g1, $g3, $g4, $g2 ), $found );
     422    }
     423
     424    /**
     425     * @group get
    399426     * @group group_meta_query
    400427     * @ticket BP5099
     
    564591    public function test_convert_orderby_to_order_by_term_last_activity() {
    565592        $c = new _BP_Groups_Group();
    566         $this->assertEquals( 'last_activity', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'last_activity' ) );
     593        $this->assertEquals( 'gm_last_activity.meta_value', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'last_activity' ) );
    567594    }
    568595
     
    572599    public function test_convert_orderby_to_order_by_term_total_member_count() {
    573600        $c = new _BP_Groups_Group();
    574         $this->assertEquals( 'CONVERT(gm1.meta_value, SIGNED)', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'total_member_count' ) );
     601        $this->assertEquals( 'CONVERT(gm_total_member_count.meta_value, SIGNED)', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'total_member_count' ) );
    575602    }
    576603
Note: See TracChangeset for help on using the changeset viewer.