MySQL optimizer hints: steer a single WordPress query without changing the server
Sometimes the MySQL optimizer picks a bad plan for one query: it uses an index merge that reads far more rows than needed, chooses the wrong index for a meta query, or joins tables in an order that explodes the intermediate result. Changing server variables affects every query. Optimizer hints, written as a special comment right after SELECT, change the plan for this one statement only, and since MySQL 8.0 they can also set a session variable just for its execution.
Problem: A WooCommerce report query filters orders by two indexed columns. EXPLAIN shows an index merge (intersection) that takes 4 seconds, while forcing the date index alone takes 80 ms. A plugin query on wp_postmeta is sometimes fast and sometimes slow because the optimizer switches between two indexes.
Solution: Confirm the better plan with EXPLAIN ANALYZE, then add a hint to the query: NO_INDEX_MERGE() or INDEX() to choose access paths, JOIN_ORDER() for the join order, SET_VAR() for a per-query variable, and apply hints in WordPress through the posts_request filter or directly in custom SQL.
Hint syntax
SELECT /*+ NO_INDEX_MERGE(o) INDEX(o date_created) */
o.id, o.total_amount
FROM wp_wc_orders AS o
WHERE o.status = 'wc-completed'
AND o.date_created_gmt >= '2026-10-01'
ORDER BY o.date_created_gmt;
The hint comment must follow SELECT, UPDATE or DELETE directly. Table names refer to the aliases in the query. An unknown or invalid hint is ignored with a warning, never an error, so a typo silently does nothing; check with SHOW WARNINGS after EXPLAIN.
Useful hints:
INDEX( t idx ),NO_INDEX( t idx ): use or avoid an index for all access types (MySQL 8.0.20+). Older variants areUSE INDEX/FORCE INDEXin theFROMclause.NO_INDEX_MERGE( t ),INDEX_MERGE( t idx1, idx2 ): control index merge.NO_RANGE_OPTIMIZATION( t idx ): disable range access for an index, useful when a hugeIN()list makes the range analysis itself slow.JOIN_ORDER( a, b, c ),JOIN_PREFIX( a ): fix the order of joined tables.SET_VAR( sort_buffer_size = 16777216 ): change a session variable only for this statement.MAX_EXECUTION_TIME( 2000 ): abort aSELECTafter 2 seconds.
Add a hint to a WP_Query
add_filter( 'posts_request', function ( string $sql, WP_Query $query ): string {
if ( 'ha_listing' !== $query->get( 'post_type' ) || ! $query->get( 'ha_hint' ) ) {
return $sql;
}
return preg_replace( '/^\s*SELECT\s+/i', 'SELECT /*+ MAX_EXECUTION_TIME(3000) NO_INDEX_MERGE(wp_postmeta) */ ', $sql, 1 );
}, 10, 2 );
$listings = new WP_Query( array(
'post_type' => 'ha_listing',
'ha_hint' => true,
'meta_query' => array( array( 'key' => 'ha_city', 'value' => 'Kraków' ) ),
) );
The custom query var ha_hint marks the queries that get the hint, so no other query of the site is affected. MAX_EXECUTION_TIME protects the site from a search page that runs for a minute; the query then returns an error instead of a result, which the page must handle. In WordPress, the table name in the hint is the real table name with prefix, or the alias that WP_Query uses (mt1, mt2 for additional meta joins).
First check, then hint
EXPLAIN ANALYZE SELECT /*+ NO_INDEX_MERGE(o) */ ... ;
Compare the actual times with and without the hint. A hint fixes the plan permanently, also when the data changes later and the optimizer’s choice would have become right. Often the better fix is an index that makes the right plan obvious, or ANALYZE TABLE to refresh statistics: EXPLAIN and EXPLAIN ANALYZE, ANALYZE and OPTIMIZE.
NOTE: Optimizer hints in comments exist since MySQL 5.7, with many additions in 8.0. MariaDB does not support this hint syntax and ignores the comments; it has its own optimizer_switch settings and USE INDEX/IGNORE INDEX clauses. A query that must run on both needs the classic index hints in the FROM clause.