Docs / Performance Optimization / How to Profile and Optimize Slow MySQL Queries

How to Profile and Optimize Slow MySQL Queries

By Admin · Feb 25, 2026 · Updated Apr 24, 2026 · 277 views · 1 min read

Slow queries are the most common cause of website performance issues.

Enable Slow Query Log

Edit /etc/mysql/mysql.conf.d/mysqld.cnf:

slow_query_log = 1\nslow_query_log_file = /var/log/mysql/slow.log\nlong_query_time = 1
systemctl restart mysql

Analyze Slow Queries

mysqldumpslow /var/log/mysql/slow.log

Use EXPLAIN

EXPLAIN SELECT * FROM users WHERE email = 'test@example.com';

Common Fixes

  • Add indexes: ALTER TABLE users ADD INDEX idx_email (email);
  • Avoid SELECT *: Only query needed columns
  • Use LIMIT: Don't fetch more rows than needed
  • Optimize JOINs: Ensure joined columns are indexed

Was this article helpful?