When a Magento store suddenly slows down, the cause is often a slow database query. MySQL can record every slow query, but this log is off by default. Switch it on before you need it.
Turn it on without restarting MySQL
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 2;
SHOW VARIABLES LIKE 'slow_query_log_file';
This records every query that takes longer than 2 seconds.
Keep it on after a restart
Add the same settings to your MySQL config, for example /etc/mysql/my.cnf under [mysqld]:
slow_query_log = 1
long_query_time = 2
On AWS RDS, set slow_query_log and long_query_time in the parameter group instead.
Read it
Summarise the slowest queries by total time:
mysqldumpslow -s t /var/log/mysql/mysql-slow.log | head -20
Look for the queries that appear most often or take the most total time. In Magento, common culprits are custom module queries without indexes, unfiltered admin grids on huge tables, and third-party reports.
Don’t over-log
Keep long_query_time at 1–2 seconds or more. Logging every query on a busy store creates gigabytes of logs and becomes a performance problem itself.
Catch it automatically
MageMonitor checks for this every few minutes as part of its Database checks, and alerts you by email, Slack or webhook the moment it appears, with the exact commands to fix it. See MageMonitor for Magento 2.
