MySQL

MySQL Optimizer Hints: INDEX, JOIN_ORDER, and MAX_EXECUTION_TIME

Guide MySQL query execution with optimizer hints. Covers INDEX, NO_INDEX, JOIN_ORDER, MAX_EXECUTION_TIME, and query block naming for subquery hint targeting.

JusDB Team
October 27, 2025
Updated June 20, 2026
5 min read

MySQL provides SQL-level optimizer hints that let you guide query execution plans without changing application code or server configuration.

Hint Syntax

sql
-- Hints go in a comment immediately after SELECT/INSERT/UPDATE/DELETE
SELECT /*+ hint_name(args) */ columns FROM table;

Index Hints

sql
-- Force a specific index
SELECT /*+ INDEX(orders idx_orders_created_at) */
  id, amount FROM orders WHERE created_at > '2025-01-01';

-- Force no index (full table scan)
SELECT /*+ NO_INDEX(orders idx_orders_status) */
  * FROM orders WHERE status = 'active';

-- Force index for ORDER BY (avoid filesort)
SELECT /*+ INDEX_ORDER(orders idx_orders_created_at) */
  * FROM orders ORDER BY created_at DESC LIMIT 100;

Join Order Hints

sql
-- Force join order (drive from orders, then join users)
SELECT /*+ JOIN_ORDER(orders, users) */
  o.id, u.email
FROM orders o JOIN users u ON o.user_id = u.id
WHERE o.status = 'pending';

-- Force nested loop join
SELECT /*+ JOIN_FIXED_ORDER() BNL(orders, users) */
  o.id, u.email
FROM orders o JOIN users u ON o.user_id = u.id;

Resource Group Hint

sql
-- Run query in a resource group (MySQL 8.0+)
SELECT /*+ RESOURCE_GROUP(analytics_group) */
  region, sum(amount) FROM orders GROUP BY region;

MAX_EXECUTION_TIME

sql
-- Kill query if it takes more than 5 seconds
SELECT /*+ MAX_EXECUTION_TIME(5000) */
  * FROM large_table WHERE complex_condition;

Query Block Naming

sql
-- Name subquery blocks to apply hints to them
SELECT /*+ QB_NAME(outer) */
  id FROM orders
WHERE user_id IN (
  SELECT /*+ QB_NAME(inner) INDEX(users idx_users_status) */ id
  FROM users WHERE status = 'premium'
);

Key Takeaways

  • Use hints to fix bad plans without changing server configuration that affects all queries
  • MAX_EXECUTION_TIME is a safety net for queries that might run away
  • Test hints with EXPLAIN first — a hint can make things worse if applied to the wrong query
  • Hints should be a last resort — fix statistics and indexes first

JusDB Can Help

MySQL query optimization often requires a combination of hints, statistics, and index design. JusDB can identify and fix your most problematic queries permanently.

Share this article

JusDB Team

Official JusDB content team

Keep reading

MySQL Explained (2026): InnoDB, 8.4 LTS, Replication & Production Patterns

Everything you need to know about MySQL: storage engines, replication topologies, performance tuning, and cloud deployment. From basics to advanced optimization.

MySQL5 minMay 13, 2026
Read

MySQL binlog Retention, Rotation & Purge: Production Guide (2026)

Configure MySQL binlog retention safely: binlog_expire_logs_seconds, manual purging rules, AWS RDS retention, and the disk-exhaustion failure mode you should monitor for.

MySQL10 minMay 9, 2026
Read

MySQL "Communications Link Failure": Fix wait_timeout, HikariCP & All 8 Timeout Variables

MySQL wait_timeout, net_read_timeout, innodb_lock_wait_timeout and max_execution_time — production tuning rules and the HikariCP alignment trick that prevents 'communications link failure' errors.

MySQL6 minMay 9, 2026
Read