Set maximum execution time in MYSQL / PHP

Set maximum execution time in MYSQL / PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Set maximum execution time in MYSQL / PHP

When processing a dataset like an XML document containing 48,000 child nodes, the resulting INSERT query can easily push a script over standard resource limits. The specific error Fatal error: Maximum execution time of 60 seconds exceeded indicates that the PHP engine has hit its internal clock before your database operations completed. While the immediate reaction is to remove this limit entirely, doing so without understanding the underlying mechanisms and trade-offs can leave your application vulnerable to timeouts or resource exhaustion.

To resolve the timeout issue, you must adjust the max_execution_time directive in your PHP configuration. This setting defines how long a script is allowed to run before being forcibly terminated. You have three primary ways to modify this value: at runtime via an .htaccess file or php.ini, or by modifying the code directly within the script itself.

If you are running on Apache, adding the following line to your .htaccess file will extend the limit for that specific directory:

php_value max_execution_time 0

Setting the value to 0 effectively disables the time limit, allowing the script to run until it finishes or hits memory limits. However, this change only applies if your server allows overriding PHP settings via .htaccess. If you are on a shared host or using Nginx with PHP-FPM, you likely cannot use .htaccess and must edit the php.ini file directly or configure it through the control panel provided by your hosting provider.

Alternatively, you can set this value programmatically at the beginning of your script using ini_set():

ini_set('max_execution_time', 0);

This method is often preferred for specific scripts because it isolates the configuration change to that single file without affecting other parts of your application. It is crucial to place this call before any heavy processing begins; once a script enters its execution phase, changing this setting might not affect the current request.

While adjusting max_execution_time solves the immediate crash, relying on unlimited execution time for large batch operations like importing 50MB of XML data introduces significant architectural risks. The primary pitfall is memory consumption. If your INSERT query processes all 48,000 rows in a single transaction without batching, it will likely trigger a Memory Limit error (Fatal error: Allowed memory size exhausted) before the time limit even becomes relevant.

To mitigate this, you should avoid loading the entire XML document into memory at once. Instead, parse the XML in chunks or stream the data. If using PHP's DOMDocument, ensure you are not creating a full tree structure for the whole file if possible, or process it node-by-node while committing smaller batches of inserts to the database. For example, instead of one massive query, loop through the nodes and execute an INSERT statement every 100 or 500 rows, followed by commit() in MySQL to free up transaction locks.

Another critical consideration is the database connection timeout. Even if PHP runs indefinitely, the network connection between PHP-FPM and MySQL might drop if it remains idle too long during a massive insert operation. You should also verify that max_execution_time in your database configuration (if applicable) and net_read_timeout are sufficient to handle the duration of the transfer.

There is a common misconception that setting the time limit to unlimited is a permanent fix for performance issues. In high-traffic environments, allowing scripts to run for minutes or hours can tie up server resources, preventing other requests from being processed. If you must process such large files, consider moving the task to a background job queue system like Laravel's Queue or Redis Jobs. This decouples the long-running process from the web request cycle, ensuring that the user experience remains responsive even if the import takes several minutes to complete.

Ultimately, setting max_execution_time to 0 is a valid technical solution for one-off data imports, but it should be paired with strategies to manage memory and transaction sizes to prevent secondary failures.

Our network