Table of contents
Get the industry’s most secure Magento hosting◦ 100% network uptime
◦ Comprehensive security
◦ 24/7 support

Magento GuideSecurity → Database Queries

How to log database queries in Magento

Magento’s database query log is a powerful tool for diagnosing performance issues and debugging custom development. It shows you every SQL query Magento runs during a page request, helping you identify bottlenecks, excessive queries, or inefficient code. The best part? Magento 2 has built-in CLI commands to enable and disable it—no core changes or risky overrides needed.

Let’s walk through how to activate the log, where to find it, how to read it, and some pro tips for putting it to use.

Host Magento at full throttle.

Get secure, reliable Magento hosting so you can scale faster.

What is Magento’s query log and when to use it

Magento’s query log records every SQL statement executed during a request cycle. It captures the full text of each query along with metadata like execution time and memory usage.

This tool is most useful in these scenarios:

Because logging every query can impact performance and expose sensitive logic, it should only be enabled in a development or staging environment.

How to enable query logging in Magento 2

Magento 2 makes it easy to toggle query logging using a built-in CLI command. This works only in Developer mode.

Step 1: Enable query logging

Run this command from your Magento root directory:

bin/magento dev:query-log:enable

Magento will create a log file at var/debug/db.log. If the folder doesn’t exist, it will be created automatically (as long as the web server has proper write permissions).

Step 2: Flush the cache

After enabling the log, flush the cache to make sure the change takes effect:

bin/magento cache:flush

Now, every SQL query Magento runs will be saved to the log file until you disable it.

How to disable query logging

Once you’re done debugging, it’s important to turn the log off:

bin/magento dev:query-log:disable

This stops the logging process but leaves the existing db.log file in place. You can delete it manually if needed, especially if it grows too large.

Where to find the query log file

Magento stores query logs in this path relative to your root directory:

/var/debug/db.log

If the file isn’t appearing:

Once active, Magento will begin writing SQL queries to this file for every request.

How to read and interpret the db.log file

Magento’s db.log file can grow quickly, but each entry contains valuable data if you know what to look for.

You’ll see output like this:

/** @var \Magento\Framework\DB\Statement\Pdo\Mysql */

SELECT e.* FROM catalog_product_entity AS e WHERE (e.entity_id = 123)

Here’s what to pay attention to:

You can use grep, less, or your code editor to search and navigate the file, or even import the contents into a database tool for advanced filtering.

Tips for debugging performance using query logs

The query log becomes especially powerful when paired with a performance mindset. Here are some ways to use it effectively:

For deeper analysis, combine query logs with Magento’s built-in profiler or third-party services like New Relic or Tideways.

How to log queries for specific Magento actions

Sometimes you only want to log queries triggered by a particular controller, route, or condition—especially useful in large or high-traffic sites.

Here are some ways to isolate the log scope:

Next steps for logging database queries in Magento

Magento’s database query logging is one of the best tools for understanding what’s really happening under the hood. Whether you’re debugging a slow page, tracking down inefficient code, or optimizing custom development, it gives you the visibility you need to fix issues quickly.

Now that you know how to enable, disable, and interpret the query log, try using it in your development environment the next time you run into a performance bottleneck.

Ready to upgrade your Magento experience? Professional hosting improves speeds, security, and reliability for a website and a brand that people find engaging and trustworthy.

Liquid Web offers the raw infrastructure power you need with mission-critical features that keep your store running smoothly. Most importantly, our in-house Magento experts are standing by to help with both hosting and Magento application roadblocks.

Click through below to explore all of our Magento hosting options, or chat with an expert right now to get answers and advice.

Ready to get started?

Get the fastest, most secure Magento hosting on the market

Additional resources

What is Magento Ecommerce? →

A complete beginner’s guide to the Magento Ecommerce platform

Magento 2 security extensions: Top 7 free and paid →

Explore top Magento 2 security extensions to protect your store from threats like malware, spam, and unauthorized access.

Best Magento ERP extensions →

Our top 10 compared so you can decide which is best for your business