Docs / Performance Optimization / How to Profile and Optimize Slow PHP Pages

How to Profile and Optimize Slow PHP Pages

By Admin · Feb 25, 2026 · Updated Apr 23, 2026 · 209 views · 1 min read

Identify Slow Pages

# Enable slow query log in PHP-FPM pool
request_slowlog_timeout = 5s
slowlog = /var/log/php8.2-fpm-slow.log
sudo systemctl restart php8.2-fpm
tail -f /var/log/php8.2-fpm-slow.log

Quick Timing in PHP

$start = microtime(true);
// ... code to profile ...
$elapsed = microtime(true) - $start;
error_log("Section took: " . round($elapsed * 1000) . "ms");

Database Query Profiling

# Enable slow query log in MySQL
SET GLOBAL slow_query_log = 1;
SET GLOBAL long_query_time = 1;
SET GLOBAL log_queries_not_using_indexes = 1;
# Analyze slow queries
mysqldumpslow -s t /var/log/mysql/slow.log | head -20

Common Bottlenecks

IssueSymptomFix
Missing DB indexQueries taking secondsAdd index, use EXPLAIN
N+1 queriesHundreds of identical queriesUse JOINs or eager loading
No OPcacheHigh CPU, slow TTFBEnable OPcache
External API callsWaiting on remote servicesCache responses, use async
Large file operationsHigh I/O waitStream instead of loading into memory

Xdebug Profiling

# php.ini
xdebug.mode=profile
xdebug.output_dir=/tmp/xdebug
xdebug.start_with_request=trigger
# Trigger profiling
curl "http://example.com/slow-page?XDEBUG_PROFILE=1"
# Open the cachegrind file with KCacheGrind or QCacheGrind

Was this article helpful?