Maximum execution time of 30 seconds exceeded

Maximum execution time of 30 seconds exceeded

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

Stefan Bogdanescu

Founder & Senior Architect

Maximum execution time of 30 seconds exceeded

When a script hits the Fatal error: Maximum execution time of 30 seconds exceeded, it is almost never because the PHP configuration actually caps execution at 30 seconds. In your specific case, you have correctly configured .htaccess to override this limit using php_value max_execution_time 3000 and php_value set_time_limit 0. The fact that the error persists indicates a fundamental misunderstanding of how PHP manages timeouts in different contexts.

The directive max_execution_time controls the maximum runtime for a script unless it runs within an asynchronous or long-running process context, but more importantly, it is often overridden by the server's own safety mechanisms or specific PHP SAPI (Server Application Programming Interface) behaviors. However, the real culprit in scenarios involving database retrieval of thousands of rows is rarely the time limit itself; it is the memory consumption and the architecture of the query execution.

When you fetch a dataset containing thousands of listings, you are likely executing a SELECT statement that loads every row into memory before returning control to your application logic. Even if PHP allows 3000 seconds (50 minutes), the script might terminate earlier due to hitting the memory_limit, which you have set to 256M. If your database query joins large tables or indexes inefficiently, the intermediate result sets can balloon in size, causing a Allowed memory size exhausted error before the time limit is even reached.

To resolve this, you must optimize the data retrieval strategy rather than simply increasing the timeout further. The most robust approach is to avoid loading entire datasets into memory at once. Instead of using a single query that returns thousands of rows, implement pagination or cursor-based fetching. For example, if your application uses Laravel (or any framework), ensure you are utilizing eager loading for relationships and limiting the result set size per request:

// Inefficient: Loads all listings into memory
$allListings = Listing::all(); 

// Efficient: Fetches in manageable chunks or paginated results
$listings = Listing::where('status', 'active')
    ->orderBy('created_at', 'desc')
    ->paginate(50); 

If you are working with a raw MySQL connection, consider using LIMIT and OFFSET to process data in batches. This reduces memory pressure significantly and prevents the script from becoming unresponsive if the dataset grows unexpectedly. Additionally, ensure your database indexes cover the columns used in your WHERE, ORDER BY, and JOIN clauses. An unindexed join on a large table can cause the query execution time to spike dramatically, potentially hitting the limit even with extended timeouts.

There is also a critical distinction between max_execution_time and memory_limit. Setting set_time_limit(0) removes the script timeout for CLI scripts or when invoked via CGI/FPM in certain configurations, but it does not grant infinite resources. If your script consumes 300MB of RAM (exceeding your 256M limit), PHP will kill the process immediately with a memory exhaustion error, regardless of how generous your time settings are.

If you have verified that your query is indexed and you are batching data correctly, but the issue persists, check your server's php.ini file directly. In shared hosting environments or some managed cloud instances, .htaccess directives might not take effect due to security restrictions or specific server configurations (e.g., mod_php vs. PHP-FPM). If max_execution_time in .htaccess is being ignored, the script runs under the global default (often 30 seconds) regardless of your configuration attempts. In such cases, you must edit the global php.ini file or use a framework-specific configuration method to set these values persistently.

Finally, consider asynchronous processing for heavy data tasks. If you are building a system where listing generation is not immediate, offload the database operation to a background job queue (like Redis with Laravel Queues). This moves the heavy lifting away from the request-response cycle entirely, ensuring your API remains responsive and avoiding timeout issues altogether.

Our network