How set limit of execution time in select, but return the result in the end
If you are using PHP try unbuffered queries and manage the timeout in PHP.
https://www.php.net/manual/en/mysqlinfo.concepts.buffering.php
Set maximum execution time in MYSQL / PHP
You can make it by setting set_time_limit in you php code (set to 0 for no limit)
set_time_limit(0);
Or modifying the value of max_execution_time directly in your php.ini
;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;
; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 120
Or changing it with ini_set()
ini_set('max_execution_time', 120); //120 seconds
but note that for this 3rd option :
max_execution_time
You can not change this setting with ini_set() when running in safe
mode. The only workaround is to turn off safe mode or by changing the
time limit in the php.ini.
Source www.php.net
MySQL - can I limit the maximum time allowed for a query to run?
Update
As of MySQL 5.7, you can include a MAX_EXECUTION_TIME
optimizer hint in your SELECT
queries to instruct the server to terminate it after the specified time.
As far as I know, if you want to enforce a server-wide timeout, or if you care about queries besides SELECT
s, the original answer is still your only option.
Original answer
There is no way to specify a maximum run time when sending a query to the server to run.
However, it is not uncommon to have a cron job that runs every second on your database server, connecting and doing something like this:
- SHOW PROCESSLIST
- Find all connections with a query time larger than your maximum desired time
- Run KILL [process id] for each of those processes
how to set mysql max_execution_time property use go
Execute SET max_execution_time=500
as a SQL statement. This limits the following SQL statements on the same connection to 0.5 seconds.
It is also possible as a SQL comment hint like SELECT /*+ MAX_EXECUTION_TIME(1000) */ field1, field2 FROM tbl ...
like
this answer.
If you show details about your slow query in a new question performance improvements may be possible.
User-specific mysql timeout
You can define this using max_execution_time
session variable (in milliseconds), something like:
SET SESSION max_execution_time=2000;
It should be done from a client for each session, so there is no way (currently) to assign this for a specific database user.
Related Topics
Escaping Ampersand Character in SQL String
Filter Table Before Applying Left Join
How to Create a Real One-To-One Relationship in SQL Server
Check If MySQL Table Exists Without Using "Select From" Syntax
Is There a Shortcut For Select * From
How to Select Unique Records by Sql
Group by to Combine/Concat a Column
String_Split in SQL Server 2012
Postgresql Group by Different from MySQL
Using the Result of an Expression (E.G. Function Call) in a Stored Procedure Parameter List
How to Output a Select Statement from a Pl/SQL Block
Best Way to Check For "Empty or Null Value"
Multiple Inner Join SQL Access
Difference Between Two Dates in MySQL
Restore Table Structure from Frm and Ibd Files
How to Delete Duplicate Entries