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

Key takeaways

  • Magento 2 includes a built-in CLI command for logging database queries during development.
  • Query logging should only run temporarily in development or staging because it can affect performance.
  • Magento writes database query logs to var/debug/db.log.
  • Disable query logging after debugging so the log file does not keep growing.

Magento’s database query log helps diagnose performance issues and debug custom development. It shows you every SQL query Magento runs during a page request, helping you identify bottlenecks, excessive queries, or inefficient code.

Enable query logging only while you’re debugging, then disable it when you’re done.

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:

  • Debugging custom extensions or modules
  • Investigating slow page loads
  • Identifying redundant or repeated queries
  • Analyzing how third-party code interacts with your database.

Before you enable query logging

Before you log database queries in Magento, confirm that you are working in development or staging when possible. Query logging can create large files quickly, and it can slow down a live store if you leave it running.

You’ll also need SSH access, access to the Magento root directory, and write permissions for var/debug.

Magento 2 query logging works in Developer mode. To check your current mode, run:

If you need to switch to Developer mode in a safe environment, run:

Avoid changing deploy modes on a live production store unless you have a specific troubleshooting plan and a rollback path.

How to enable query logging in Magento 2

This command works only in Developer mode.

Step 1: Enable query logging

Run this command from your Magento root directory:

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:

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

How to view the Magento database query log

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

To watch database queries as you reproduce an issue, run:

Use this while you click through the specific page, Admin action, or checkout step you want to test. Then stop watching the file once you have the data you need.

How to disable query logging

Disabling query logging stops new entries from being written. The existing db.log file may remain in place, so remove or archive it if it becomes large.

Use care before deleting logs if your team still needs them for debugging.

What to do if the query log file doesn’t appear

If var/debug/db.log does not appear, check the basics first:

  • Confirm Magento is in Developer mode
  • Flush the cache after enabling query logging
  • Make sure var/debug exists or can be created
  • Confirm the server user can write to var/debug
  • Check file ownership and permissions
  • Review security tools such as SELinux or AppArmor if they may block writes

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:

Here’s what to pay attention to:

  • Query type: SELECT, INSERT, UPDATE, DELETE. SELECTs are common for read operations
  • Query repetition: Look for identical queries running more than once in the same request
  • Wildcards: SELECT * can cause unnecessary load. Look for specific column selection instead
  • Join complexity: Multiple joins, especially unindexed ones, slow performance
  • Execution time: Not always included by default, but can be logged with profiling tools

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.

Optional advanced query logging configuration

Most users should start with the Magento CLI command. Advanced users can configure query logging through dependency injection if they need more control over what Magento logs.

For example, a developer may configure Magento\Framework\DB\Logger\File to log all queries, log only slow queries, include call stacks, or write to a specific debug file.

After editing dependency injection configuration, clean the cache:

Test DI changes in development or staging, then remove them when you finish debugging.

Magento 1 legacy note

Magento 1 used a different query logging approach through app/etc/local.xml and the <profiler> flag.

Magento 1 query output may write to:

Magento 1 has reached end of life, so this guidance only applies to legacy stores still under special maintenance.

Tips for debugging performance using query logs

  1. Slow page loads: Filter for queries taking the longest or appearing most often.
  2. Heavy extensions: Identify third-party modules by looking at table prefixes or query patterns.
  3. Unindexed fields: Use EXPLAIN in MySQL to test slow queries for missing indexes.

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

  • Temporarily enable query logging in PHP. Add the following to your custom controller or observer:
  • Use conditional logic. Log queries only for certain admin users or request paths:
  • Custom logging. Create your own logger class to write query output to a separate file for targeted debugging.
  • Use breakpoints in Xdebug. Step through query execution during a request to see exactly what code triggered it.

Targeted logging can reduce noise, but keep it temporary. Remove custom logging code when you finish testing.

When to ask for support

Ask for support if the query log shows repeated slow queries, database load stays high, the log file grows quickly, or a suspected extension creates database performance issues.

Support can help review hosting resources, database load, logs, and whether the issue comes from Magento code, extensions, or server configuration.

Magento database queries FAQs

Common causes include Magento not being in Developer mode, cache not being flushed, or var/debug not being writable.

Yes, advanced users can configure query logging settings such as logQueryTime through dependency injection. Test that type of change outside production first.

Getting started with Magento database queries

Magento query logging helps you find slow, repeated, or inefficient database queries, but you should use it temporarily and in the right environment.

Start by enabling query logging in development or staging, reproduce the issue, review var/debug/db.log, then disable query logging and clear cache.

If database query issues point to hosting resources, database load, or Magento performance problems, explore Liquid Web Magento hosting.

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