How do I print all the queries in Magento?

How do I print all the queries in Magento?

August 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How do I print all the queries in Magento?

Displaying All Query Strings in Magento

As an e-commerce developer, you may often wonder what SQL queries are being executed behind the scenes in your Magento store. Having visibility into these queries can help identify performance bottlenecks, debug issues, and optimize database interactions.

Enabling Logs for Queries

Magento provides a built-in feature to log all query strings. To enable this:

  1. Navigate to the app/Mage.php file.
  2. Locate the line $_server = Varien_Http_Client::server();.
$logQueryTime = (bool) Mage::getConfig()->getNode('global Resource/Connection/log_query_time');
  1. Change this line to enable query logging:
$logQueryTime = true;
  1. Save the file and clear your cache.

Viewing the Query Logs

After enabling the logs, Magento will start recording all executed queries along with their execution time in a log file located at var/log/magento.log.

To view these logs:

  1. Open the var/log/magento.log file using any text editor.
  2. Look for the lines starting with SQLSTATE to find all recorded queries.
2023-04-15T12:00:36+00:00 CRIT (6): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mystore.core_config_data' doesn't exist, query was: SELECT * FROM `core_config_data` WHERE `path` = 'general/locale/code'

Each line displays the SQL state, an error code (if applicable), and the executed query. The execution time is also logged if you enabled the $logQueryTime setting.

Analyzing the Logs

The logs provide valuable insights into your Magento store’s database interactions:

  • Identify Slow Queries: Look for queries with high execution times to optimize them.
  • Detect Missing Tables/Views: Errors like “Table not found” indicate missing or misconfigured database tables.
  • Review Third-Party Extensions: Some extensions may execute inefficient or unnecessary queries.

Regularly reviewing these logs can help maintain a well-performing Magento store and identify areas for optimization.

Conclusion

Enabling and analyzing query logging in Magento is a fundamental practice for e-commerce developers. By following the steps outlined above, you gain full visibility into your store’s database operations, which can significantly aid in debugging, performance tuning, and overall system health monitoring.

This technique works well with our recommended approach at Laravel Company, where we emphasize transparency and data-driven decision-making for our clients’ e-commerce platforms.

Cache Database Performance Configuration

Our network