0Pricing
PHP Academy · Lesson

Optimizing Database Queries

Identify N+1 queries, add indexes, and reduce query count.

Identify Slow Queries

Enable the MySQL slow query log and set a threshold. Review queries taking longer than expected.

# my.cnf:
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 0.5  # log queries over 500ms

EXPLAIN / EXPLAIN ANALYZE

Use EXPLAIN to understand how MySQL executes a query and whether indexes are used.

-- Run in MySQL:
EXPLAIN SELECT * FROM orders WHERE user_id = 42 AND status = "pending";
-- Look for: type=ALL (full scan), key=NULL (no index used)

All lessons in this course

  1. Profiling PHP with Xdebug
  2. OPcache: Bytecode Caching
  3. Optimizing Database Queries
  4. Memory Management and Performance Tips
← Back to PHP Academy