◦ Comprehensive security
◦ 24/7 support
Magento Guide → Security → 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:
- Debugging custom extensions or modules
- Investigating slow page loads
- Identifying redundant or repeated queries
- Analyzing how third-party code interacts with your database
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:
- Confirm Magento is in Developer mode:
bin/magento deploy:mode:show - Make sure the var/debug folder is writable by the server user.
- Check for permission or ownership issues using ls -l and chmod/chown as needed.
- If using SELinux or AppArmor, check for blocked writes.
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:
- 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.
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:
- Slow page loads: Filter for queries taking the longest or appearing most often.
- Heavy extensions: Identify third-party modules by looking at table prefixes or query patterns.
- Excessive joins: Spot queries joining too many tables—often the root of slowdowns on product or category pages.
- Repeated queries: Catch N+1 query patterns (e.g., running the same query in a loop).
- 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
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:
- Temporarily enable query logging in PHP. Add the following to your custom controller or observer:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$logger = $objectManager->get(\Magento\Framework\DB\Logger\File::class);
$logger->setLogAllQueries(true);
- Use conditional logic. Log queries only for certain admin users or request paths:
if ($request->getFullActionName() === ‘catalog_product_edit’) {
$logger->setLogAllQueries(true);
}
- 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.
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