When a Magento 2 store is slow, PHP, Redis and Varnish are not always to blame. Individual slow database queries can also make certain pages or processes much slower than expected.
Magento 2 includes a Query Log that you can enable directly from the CLI:
|
1 |
bin/magento dev:query-log:enable |
Recorded queries are written to var/debug/db.log and can be followed live with:
|
1 |
tail -f var/debug/db.log |
If you do not want to log every small query, Magento can also record only those above a given duration:
|
1 2 3 4 |
bin/magento dev:query-log:enable \ --include-all-queries=false \ --query-time-threshold=0.5 \ --include-call-stack=true |
In this example, only database queries taking at least 0.5 seconds are logged. The include-call-stack option also records the call stack, helping you identify which module or code triggered a slow query.
Especially for slow product pages, imports, cron jobs and custom extensions, this can reveal whether one database query is the bottleneck.
Disable the query log again after your analysis:
|
1 |
bin/magento dev:query-log:disable |
With call stack logging enabled, the file may otherwise grow very quickly.