MAX_EXECUTION_TIME hint and SELECT statement termination granularity
0 reputation · 21 May 2020, 07:20 UTC
0 reputation · 21 May 2020, 07:20 UTC
MySQL 5.7.7+ provides the MAX_EXECUTION_TIME(N) optimizer hint to limit the execution duration of SELECT statements in milliseconds. While this mechanism prevents runaway read queries, the enforcement occurs at specific checkpoints during the execution process.
The timeout check is typically performed between row fetches. This creates a potential discrepancy between the requested millisecond limit and the actual time the server takes to terminate a query, particularly during complex operations that do not trigger frequent row-fetch checkpoints.
Because this hint is restricted to SELECT statements, other data modification language (DML) operations require different timeout configurations to avoid long-running locks or resource exhaustion.
MAX_EXECUTION_TIME hint?SELECT statement exceed the defined millisecond limit before the server triggers error 3024?34775 reputation · 21 May 2020, 14:50 UTC
1. Granularity across storage engines
2. Conditions that allow a SELECT to exceed the defined millisecond limit before error 3024
SELECT COUNT(*) FROM (SELECT * FROM big_table) AS t) – the hint is only evaluated after the temporary structure is fully built.SELECT SUM(col) FROM table) – the timeout checkpoint is reached only after the aggregation finishes.FROM clause that force early materialization.CREATE TABLE t_innodb (id INT PRIMARY KEY, val INT) ENGINE=InnoDB;
CREATE TABLE t_myisam (id INT PRIMARY KEY, val INT) ENGINE=MyISAM;
-- (If you have NDB, create a similar table with ENGINE=NDB)
INSERT INTO t_innodb SELECT n, n FROM seq_1_to_1000000; -- adjust size
INSERT INTO t_myisam SELECT n, n FROM seq_1_to_1000000;
SET SESSION performance_schema = ON;
SELECT * FROM t_innodb WHERE id > 500000 WITH MAX_EXECUTION_TIME(100);
-- Check performance_schema.events_statements_current for elapsed_time
SELECT COUNT(*) FROM (SELECT * FROM t_innodb) AS sub WITH MAX_EXECUTION_TIME(200);
-- Session A
START TRANSACTION;
UPDATE t_innodb SET val = val + 1 WHERE id = 1;
-- Session B (immediately)
SELECT * FROM t_innodb WITH MAX_EXECUTION_TIME(50); -- will wait until the lock is released
Observe that in the first two tests the query terminates close to the specified limit, while in the temporary‑table and lock‑wait tests the actual elapsed time can exceed the hint value by a noticeable margin before error 3024 is raised.
Verify whether your query triggers a temporary table, filesort, or lock wait. If so, the hint’s granularity will be coarser, and you may need to adjust the limit or rewrite the query to reduce materialization.
To tailor the recommendation further, could you provide the specific query that is exceeding the timeout? Knowing whether it involves aggregation, subqueries, or locks will help refine the guidance.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.