Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-30 · container image mysql:8.4
max_execution_time (milliseconds, 0 = no limit) and the /*+ MAX_EXECUTION_TIME(n) */ hint stop a SELECT that runs too long. When the limit is hit, MySQL ends the query with ERROR 3024 and returns no rows. Raising the limit hides the problem; both cases below made the query itself fast enough instead.
The tests used 2,000 customers and 100,000 orders on MySQL 8.4. This message stays in English even with Korean or Japanese lc_messages.
01Session limit 1 second + correlated subquery on a column with no index
With SET SESSION max_execution_time = 1000, a query summing each customer's orders in a subquery (WHERE o.customer_id = c.id) failed with ERROR 3024. information_schema.STATISTICS showed only PRIMARY, so every customer scanned all orders.
After CREATE INDEX idx_orders_customer ON orders (customer_id), the same query with the same 1-second limit returned 3 rows in 0.21 sec.
Setup
SET SESSION cte_max_recursion_depth = 100000;
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(20));
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, amount INT);
INSERT INTO customers WITH RECURSIVE r(v) AS (SELECT 1 UNION ALL SELECT v + 1 FROM r WHERE v < 2000) SELECT v, CONCAT('cust', v) FROM r;
INSERT INTO orders WITH RECURSIVE r(v) AS (SELECT 1 UNION ALL SELECT v + 1 FROM r WHERE v < 100000) SELECT v, v % 2000 + 1, v % 100 FROM r;Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 2000 rows affected (0.02 sec)
Records: 2000 Duplicates: 0 Warnings: 0
Query OK, 100000 rows affected (0.31 sec)
Records: 100000 Duplicates: 0 Warnings: 0SET SESSION max_execution_time = 1000;
SELECT c.id, c.name,
(SELECT SUM(o.amount) FROM orders o WHERE o.customer_id = c.id) AS total
FROM customers c ORDER BY total DESC, c.id LIMIT 3;Query OK, 0 rows affected (0.00 sec)
ERROR 3024 (HY000): Query execution was interrupted, maximum statement execution time exceededSELECT INDEX_NAME, COLUMN_NAME FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'lab_a' AND TABLE_NAME = 'orders' ORDER BY INDEX_NAME;+------------+-------------+
| INDEX_NAME | COLUMN_NAME |
+------------+-------------+
| PRIMARY | id |
+------------+-------------+
1 row in set (0.00 sec)CREATE INDEX idx_orders_customer ON orders (customer_id);Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0Check EXPLAIN for type ALL on the inner table of a subquery or join. An index on the join column is usually the fix.
SET SESSION max_execution_time = 1000;
SELECT c.id, c.name,
(SELECT SUM(o.amount) FROM orders o WHERE o.customer_id = c.id) AS total
FROM customers c ORDER BY total DESC, c.id LIMIT 3;Query OK, 0 rows affected (0.00 sec)
+------+---------+-------+
| id | name | total |
+------+---------+-------+
| 100 | cust100 | 4950 |
| 200 | cust200 | 4950 |
| 300 | cust300 | 4950 |
+------+---------+-------+
3 rows in set (0.21 sec)FAQ
How do I fix maximum statement execution time exceeded in MySQL?
Make the query faster: add an index on the join or WHERE column, or rewrite a per-row subquery as a JOIN with GROUP BY. Both were enough here without raising the limit.
Where does the limit come from?
From the max_execution_time variable (in milliseconds, 0 means no limit) or a /*+ MAX_EXECUTION_TIME(n) */ hint in the query. Check SELECT @@max_execution_time and the query text.
How do I find why the query is slow?
Run EXPLAIN on it. Here it showed DEPENDENT SUBQUERY on orders with type ALL and rows 100000: a full scan repeated for every customer.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13