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:
@@ -48,12 +48,7 @@ function tmdo_live_router_query( array $args, array $ids, bool $routed ): array
|
||||
)
|
||||
);
|
||||
} finally {
|
||||
foreach ( array( 'pre_get_posts', 'posts_join', 'posts_where', 'posts_orderby', 'posts_request', 'found_posts_query' ) as $hook ) {
|
||||
$priority = has_filter( $hook, array( $router, $hook ) );
|
||||
if ( false !== $priority ) {
|
||||
remove_filter( $hook, array( $router, $hook ), $priority );
|
||||
}
|
||||
}
|
||||
tmdo_live_router_unhook( $router );
|
||||
}
|
||||
tmdo_live_assert_same( '', $wpdb->last_error, 'SQL error' );
|
||||
return array(
|
||||
@@ -63,6 +58,22 @@ function tmdo_live_router_query( array $args, array $ids, bool $routed ): array
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove every hook TMDO_Post_Query_Router::register_hooks() added.
|
||||
*
|
||||
* @param TMDO_Post_Query_Router $router Router instance.
|
||||
*/
|
||||
function tmdo_live_router_unhook( TMDO_Post_Query_Router $router ): void {
|
||||
foreach ( array( 'pre_get_posts', 'posts_join', 'posts_where', 'posts_orderby', 'posts_request', 'split_the_query', 'found_posts_query' ) as $method ) {
|
||||
foreach ( 'posts_request' === $method ? array( 'posts_request', 'posts_request_ids' ) : array( $method ) as $hook ) {
|
||||
$priority = has_filter( $hook, array( $router, $method ) );
|
||||
if ( false !== $priority ) {
|
||||
remove_filter( $hook, array( $router, $method ), $priority );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create posts of $post_type with the given meta, in post mode shadow_read
|
||||
* (so the Hook Bus keeps the flat rows in step).
|
||||
@@ -340,3 +351,33 @@ TMDO_Live::test(
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
TMDO_Live::test(
|
||||
'a routed full-row search keeps the split query (IDs first) and native found_posts',
|
||||
static function () {
|
||||
global $wpdb;
|
||||
$l = tmdo_live_router_listings();
|
||||
$args = array(
|
||||
'post_type' => 'hp_listing',
|
||||
'post__in' => array_values( $l ),
|
||||
'post_status' => 'any',
|
||||
'posts_per_page' => 2,
|
||||
'orderby' => 'ID',
|
||||
'order' => 'ASC',
|
||||
'meta_query' => array( array( 'key' => 'hp_price', 'value' => 1000, 'compare' => '>=', 'type' => 'NUMERIC' ) ),
|
||||
);
|
||||
$native = new WP_Query( $args );
|
||||
$router = new TMDO_Post_Query_Router();
|
||||
$router->register_hooks();
|
||||
try {
|
||||
$routed = new WP_Query( $args );
|
||||
} finally {
|
||||
tmdo_live_router_unhook( $router );
|
||||
}
|
||||
|
||||
tmdo_live_assert_same( wp_list_pluck( $native->posts, 'ID' ), wp_list_pluck( $routed->posts, 'ID' ), 'page of posts' );
|
||||
tmdo_live_assert_same( $native->found_posts, $routed->found_posts, 'found_posts' );
|
||||
tmdo_live_assert_true( false !== strpos( $routed->request, 'wpdoflat_' ), 'routed: ' . $routed->request );
|
||||
tmdo_live_assert_true( 1 === preg_match( '/^\s*SELECT\s+' . preg_quote( $wpdb->posts, '/' ) . '\.ID\s/', $routed->request ), 'IDs first: ' . $routed->request );
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user