perf(2meet-data-optimizer): keep WordPress's split query on routed searches
- posts_request only drops SQL_CALC_FOUND_ROWS, but WordPress then treated the request as filtered and selected full post rows through a temporary table; split_the_query now keeps the ID-first split for routed searches when nothing else changed the request, and posts_request_ids drops SQL_CALC_FOUND_ROWS from the split ID query - Verify: tests/live/run.php router (12/12, IDs-first request, found_posts == native); dev36 fe.sh price 589→340 ms, price__asc 961→427 ms, rating__desc 1724→872 ms, hourly_rate__desc 855→884 ms (was 1206) - PR: PR-062b
This commit is contained in:
@@ -133,4 +133,49 @@ class PostQueryRouterFoundRowsTest extends TestCase {
|
||||
$this->assertNotSame( $before, $after_write );
|
||||
$this->assertNotSame( $after_write, wp_cache_get_last_changed( 'posts' ) );
|
||||
}
|
||||
|
||||
// ── split query: IDs first, then cached post objects ──────────────────
|
||||
|
||||
/**
|
||||
* WordPress's full-row request for a routed query, before and after posts_request.
|
||||
*
|
||||
* @return array{0:TMDO_Post_Query_Router, 1:WP_Query, 2:string, 3:array<string, string>}
|
||||
*/
|
||||
private function full_rows(): array {
|
||||
global $wpdb;
|
||||
list( $router, $query, $request ) = $this->routed();
|
||||
$old_request = str_replace( "{$wpdb->posts}.ID FROM", "{$wpdb->posts}.* FROM", $request );
|
||||
$query->request = $router->posts_request( $old_request, $query );
|
||||
$query->set( 'posts_per_page', 1 );
|
||||
return array( $router, $query, $old_request, array( 'fields' => "{$wpdb->posts}.*", 'limits' => 'LIMIT 0, 1' ) );
|
||||
}
|
||||
|
||||
public function test_routed_full_row_request_is_still_split(): void {
|
||||
list( $router, $query, $old_request, $clauses ) = $this->full_rows();
|
||||
|
||||
$this->assertTrue( $router->split_the_query( false, $query, $old_request, $clauses ) );
|
||||
}
|
||||
|
||||
public function test_request_changed_by_others_is_not_split(): void {
|
||||
list( $router, $query, $old_request, $clauses ) = $this->full_rows();
|
||||
$query->request .= ' /* another plugin */';
|
||||
|
||||
$this->assertFalse( $router->split_the_query( false, $query, $old_request, $clauses ) );
|
||||
}
|
||||
|
||||
public function test_unrouted_query_keeps_the_core_decision(): void {
|
||||
$router = new TMDO_Post_Query_Router();
|
||||
|
||||
$this->assertFalse( $router->split_the_query( false, new WP_Query(), 'SELECT 1', array( 'fields' => 'wp_itest_posts.*', 'limits' => 'LIMIT 0, 1' ) ) );
|
||||
$this->assertTrue( $router->split_the_query( true, new WP_Query(), 'SELECT 1', array() ) );
|
||||
}
|
||||
|
||||
public function test_split_id_request_goes_through_posts_request_too(): void {
|
||||
unset( $GLOBALS['_wp_filter_callbacks']['posts_request_ids'], $GLOBALS['_wp_filter_callbacks']['split_the_query'] );
|
||||
$router = new TMDO_Post_Query_Router();
|
||||
$router->register_hooks();
|
||||
|
||||
$this->assertContains( array( $router, 'posts_request' ), $GLOBALS['_wp_filter_callbacks']['posts_request_ids'] ?? array() );
|
||||
$this->assertContains( array( $router, 'split_the_query' ), $GLOBALS['_wp_filter_callbacks']['split_the_query'] ?? array() );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user