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:

Recorded queries are written to var/debug/db.log and can be followed live with:

If you do not want to log every small query, Magento can also record only those above a given duration:

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:

With call stack logging enabled, the file may otherwise grow very quickly.

Finding Slow Database Queries with the Query Log
Need help implementing this solution, or still looking for a fix to your Magento problem? Get in touch and I will be happy to help with your project.
Tags: