Skip to main content

How to Limit How Long a SELECT Runs in MySQL

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A report with a bad join can run for hours and take the server down with it. max_execution_time tells MySQL to stop a SELECT after a set time. The limit lives on the server, so it works no matter which program sent the query.

Prerequisites​

Step 1: Check the Current Limit​

mysql
SELECT @@max_execution_time;
Output
+----------------------+
| @@max_execution_time |
+----------------------+
| 0 |
+----------------------+
1 row in set (0.000 sec)

0 means no limit, which is the default. The value is in milliseconds.

Step 2: Set It for Your Session​

mysql
SET SESSION max_execution_time = 1000;

Now run something slow. This one joins a large system table to itself three times:

mysql
SELECT COUNT(*) FROM information_schema.columns a, information_schema.columns b, information_schema.columns c;
Output
ERROR 3024 (HY000): Query execution was interrupted, maximum statement execution time exceeded

A mysql session with max_execution_time set to 1000, where a slow SELECT fails with ERROR 3024 Query execution was interrupted, maximum statement execution time exceeded.

One second, and MySQL stopped it. Fixed: ERROR 3024 covers the error.

SLEEP is the exception to watch for when you test this. It's interrupted, and it reports that with a return value instead of an error:

mysql
SELECT SLEEP(3);
Output
+----------+
| SLEEP(3) |
+----------+
| 1 |
+----------+
1 row in set (1.007 sec)

It returned after one second, and the 1 means it was cut short. Normally it returns 0.

Step 3: Set It for One Query​

An optimizer hint applies to one statement. It goes right after SELECT, in a special comment:

mysql
SET SESSION max_execution_time = 0;
SELECT /*+ MAX_EXECUTION_TIME(1000) */ COUNT(*) FROM information_schema.columns a, information_schema.columns b, information_schema.columns c;
Output (the SELECT)
ERROR 3024 (HY000): Query execution was interrupted, maximum statement execution time exceeded

The optimizer hints page says: "The MAX_EXECUTION_TIME hint is permitted only for SELECT statements."

Step 4: Set It for the Server​

An administrator can set the default for new connections:

mysql: root session
SET GLOBAL max_execution_time = 30000;

The system variables page says: "The global max_execution_time value provides the default for the session value for new connections." To set it from an application, for each connection it opens: How to Run SQL When a Connection Opens in EF Core.

What It Doesn't Cover​

It's only for read-only SELECT statements. An UPDATE or DELETE that runs for a long time isn't stopped by it. Waiting for a row lock has its own limit, innodb_lock_wait_timeout.

Common Mistakes​

MistakeWhat happensDo this instead
Treating the value as seconds30 means 30 millisecondsUse 30000 for 30 seconds
Testing it with SLEEPYou get 1, and no errorTest with a real slow query
Expecting it to stop an UPDATEIt only covers read-only SELECTsUse a smaller WHERE, or KILL

Troubleshooting​

SymptomLikely causeFix
Query execution was interrupted, maximum statement execution time exceededThe SELECT ran past the limitThe fix
A SELECT ran longer than the limitIt's inside a stored program, or it's waiting for a lockCheck the page's rules
The hint does nothingIt isn't right after SELECTMove it

Conclusion​

SET SESSION max_execution_time = 1000 limits this session's SELECTs to one second, and /*+ MAX_EXECUTION_TIME(1000) */ limits one query. It works on the server, so it applies whichever program sent the query. Remember that it's in milliseconds, and that writes aren't covered.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.