Help: my PHP script keeps timing out on large queries
|
admin
Joined: Feb 2026
|
Feb 7, 2026 at 12:09 pm
I have a PHP script that generates reports from a MySQL database. When the query returns more than about 5000 rows, the script times out at the 30 second limit. I've tried set_time_limit(0) but my host blocks that. Any clever solutions? I can't change the php.ini settings. |
|
admin
Joined: Feb 2026
|
Feb 7, 2026 at 2:09 pm
Paginate the query. Instead of selecting all 5000 rows at once, use LIMIT and OFFSET to process them in batches of 500. Loop through batches and output as you go. |
|
admin
Joined: Feb 2026
|
Feb 7, 2026 at 4:09 pm
Also make sure you have proper indexes on the columns you're querying. A missing index on a WHERE clause column can turn a 0.1 second query into a 30 second one. |
|
admin
Joined: Feb 2026
|
Feb 7, 2026 at 6:09 pm
Look into mysql_unbuffered_query() instead of mysql_query(). It starts returning results immediately without buffering the whole result set in memory. |
Log in to post a reply.