Home >Database >Mysql Tutorial >How Can I Set a Maximum Execution Time for MySQL Queries?

How Can I Set a Maximum Execution Time for MySQL Queries?

Barbara Streisand
Barbara StreisandOriginal
2025-01-05 12:54:40208browse

How Can I Set a Maximum Execution Time for MySQL Queries?

Setting Maximum Execution Time for MySQL Queries

In PHP, the set_time_limit() function allows developers to limit the maximum execution time of scripts. However, there is no direct equivalent for setting such limits for MySQL queries.

Historically, MySQL didn't provide a way to enforce execution time limits. However, this changed with the introduction of MySQL 5.7.4.

Introducing max_execution_time

In MySQL 5.7.4, the max_execution_time variable was introduced. This variable allows you to specify the maximum execution time in milliseconds for top-level read-only SELECT statements.

To use this feature, you can add the following syntax to your SELECT statement:

SELECT 
/*+ MAX_EXECUTION_TIME(1000) */ --in milliseconds
* 
FROM table;

This will set the maximum execution time for the query to 1000 milliseconds (1 second).

Note: This feature only applies to read-only SELECT statements.

Renaming to max_execution_time

In MySQL 5.7.8, the max_execution_time variable was renamed to max_execution_time for consistency.

Additional Information

  • The max_execution_time variable can be set globally or per session.
  • The default value for max_execution_time is 0, which means no time limit.
  • If the execution time of a query exceeds the specified limit, MySQL will terminate the query and return an error.

The above is the detailed content of How Can I Set a Maximum Execution Time for MySQL Queries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn