perf(2meet-data-optimizer): drive flat-sorted searches from the flat table

- A filtered search ordered by a flat column is rewritten to SELECT STRAIGHT_JOIN … FROM flat INNER JOIN wp_posts, with posts.ID tie-breaks (ours and plugin-appended) read as the equal flat post_id, so MariaDB sorts one table instead of looking every post up and sorting in a temporary table
- Only when the routed conditions already require a flat row on the sort group, without the copy route or a selective post filter; filter tmdo_router_flat_first turns it off; split_the_query compares with the same rewrite
- Verify: tests/live/run.php router (13/13); integration 522; dev36 page SQL Q07 263→147 ms, Q15 250→142 ms; fe.sh hourly_rate__desc 850→367 ms, ids == native
- PR: PR-062c
This commit is contained in:
2026-09-26 21:05:11 +08:00
parent a7134bfb80
commit dc9a8e9886
3 changed files with 206 additions and 5 deletions
@@ -337,10 +337,14 @@ class TMDO_Post_Query_Router {
public function posts_request( ?string $request, \WP_Query $query ): string {
$request = (string) ( $request ?? '' );
$routed = ! empty( $query->get( self::QUERY_VAR ) ) || ! empty( $query->get( self::SORT_VAR ) );
if ( ! $routed || false === strpos( $request, 'SQL_CALC_FOUND_ROWS' ) ) {
if ( ! $routed ) {
return $request;
}
$counted = false !== strpos( $request, 'SQL_CALC_FOUND_ROWS' );
$request = self::routed_request( $request, $query );
if ( ! $counted ) {
return $request;
}
$request = self::without_found_rows( $request );
$matches = preg_replace( '/\s+LIMIT\s+\d+(\s*,\s*\d+)?\s*$/i', '', $request );
$query->set( self::COUNT_VAR, "SELECT COUNT(*) FROM ( {$matches} ) AS tmdo_found" );
return $request;
@@ -367,7 +371,7 @@ class TMDO_Post_Query_Router {
global $wpdb;
// Core's other conditions: full rows, and a LIMIT under 500 or an external object cache.
return "{$wpdb->posts}.*" === ( $clauses['fields'] ?? '' )
&& self::without_found_rows( (string) $old_request ) === $query->request
&& self::routed_request( (string) $old_request, $query ) === $query->request
&& ( ( '' !== ( $clauses['limits'] ?? '' ) && (int) $query->get( 'posts_per_page' ) < 500 ) || wp_using_ext_object_cache() );
}
@@ -569,6 +573,81 @@ class TMDO_Post_Query_Router {
);
}
/**
* What posts_request makes of a routed request.
*
* @param string $request SELECT statement.
* @param \WP_Query $query Current WP_Query.
* @return string
*/
private static function routed_request( string $request, \WP_Query $query ): string {
return self::flat_first( self::without_found_rows( $request ), $query );
}
/**
* Drive a filtered search ordered by a flat column from its flat table.
*
* MariaDB starts from wp_posts (the type / status index), looks every
* candidate up in the flat table and sorts in a temporary table. From the
* flat table, with the posts.ID tie-break read as the equal flat post_id,
* it sorts one table (dev36, 100k listings: meta_value_num 263 → 147 ms,
* HivePress sort + filter 300 → 36 ms). Only when the routed conditions
* already require a flat row (LEFT JOIN = INNER JOIN), all on the sort
* group, without a selective post filter; `tmdo_router_flat_first` can
* turn it off.
*
* @param string $request SELECT statement.
* @param \WP_Query $query Current WP_Query.
* @return string
*/
private static function flat_first( string $request, \WP_Query $query ): string {
$routed = $query->get( self::QUERY_VAR );
$sort = $query->get( self::SORT_VAR );
if ( empty( $routed ) || ! is_array( $routed ) || ! empty( $query->get( self::COPY_VAR ) ) ) {
return $request;
}
$post_type = (string) array_key_first( $routed );
$groups = array_unique( array_column( $routed[ $post_type ], 'group' ) );
$compares = array_diff( array_column( $routed[ $post_type ], 'compare' ), array( 'NOT EXISTS' ) );
if ( empty( $sort ) || 1 !== count( $groups ) || empty( $compares ) || $sort['group'] !== $groups[0] ) {
return $request;
}
foreach ( array( 'p', 'page_id', 'name', 'pagename', 'attachment_id', 'post__in', 'post_parent', 'post_parent__in', 'author', 'author__in', 'author_name' ) as $var ) {
if ( ! empty( $query->get( $var ) ) ) {
return $request;
}
}
if ( ! apply_filters( 'tmdo_router_flat_first', true, $query ) ) {
return $request;
}
global $wpdb;
$table = $wpdb->prefix . 'wpdo_post_' . sanitize_key( $groups[0] );
$alias = self::table_alias( $post_type, $groups[0] );
$on = "(`{$wpdb->posts}`.`ID` = `{$alias}`.`post_id`)";
$join = " LEFT JOIN `{$table}` AS `{$alias}` ON {$on}";
if ( 1 !== substr_count( $request, $join ) ) {
return $request;
}
$flat_first = preg_replace(
'/\bFROM\s+' . preg_quote( $wpdb->posts, '/' ) . '\b/',
"FROM `{$table}` AS `{$alias}` INNER JOIN {$wpdb->posts} ON {$on}",
str_replace( $join, '', $request ),
1,
$replaced
);
if ( 1 !== $replaced ) {
return $request;
}
// Tie-breaks on posts.ID (ours, and any a plugin appended) read the flat post_id.
$flat_first = preg_replace_callback(
'/\bORDER BY\b(?!.*\bORDER BY\b).*$/s',
static fn( $m ) => preg_replace( '/`?' . preg_quote( $wpdb->posts, '/' ) . '`?\.`?ID`?(?!\w)/', "`{$alias}`.`post_id`", $m[0] ),
(string) $flat_first
);
return (string) preg_replace( '/^\s*SELECT\s/', 'SELECT STRAIGHT_JOIN ', (string) $flat_first, 1 );
}
/**
* A request without its SQL_CALC_FOUND_ROWS.
*