Setting Execution Time Limits for MySQL Queries
Limiting the execution time of MySQL queries can be crucial for performance and resource optimization. This guide explores how to set a maximum execution time for MySQL queries, providing a solution that is similar to the set_time_limit() function in PHP.
Solution for MySQL 5.7.4 and Later
In MySQL 5.7.4 and subsequent versions, a new feature was introduced allowing the specification of an execution time limit for read-only SELECT statements at the server level. This limit is set in milliseconds using the MAX_EXECUTION_TIME hint:
SELECT /*+ MAX_EXECUTION_TIME(1000) */ * FROM table;
Note: This feature is only applicable to read-only SELECT statements.
Updated Variable Name
In MySQL 5.7.8, the MAX_EXECUTION_TIME variable was renamed to max_execution_time. Therefore, for MySQL 5.7.8 and later, the syntax is as follows:
SELECT /*+ max_execution_time(1000) */ * FROM table;
The above is the detailed content of How to Limit MySQL Query Execution Time?. For more information, please follow other related articles on the PHP Chinese website!