How to log all Magento SQL?

If you want to be sure all SQL is actually logged, I suggest to use a third party software:

  • Neon Profile SQL is actually free and works fine
    ( you need to edit local.xml to attach Magento to Neon ... it works as a kind of SQL proxy logging everything that pass through it)

In alternative a native Magento/Varien approach could be the following one:

  1. edit lib/Varien/Db/Adapter/Pdo/Mysql.php
  2. change the following properties to true (line 103 )
  3. you will have a log file to be created here var/debug/pdo_mysql.log

Here line to be changed: ( comments are self explanatory )

 /**
     * Write SQL debug data to file
     *
     * @var bool
     */
    protected $_debug               = true;

    /**
     * Minimum query duration time to be logged
     *
     * @var float
     */
    protected $_logQueryTime        = 0.05;

    /**
     * Log all queries (ignored minimum query duration time)
     *
     * @var bool
     */
    protected $_logAllQueries       = true;

    /**
     * Add to log call stack data (backtrace)
     *
     * @var bool
     */
    protected $_logCallStack        = true;

Log produced are like the following one:

## 2014-06-26 12:18:47
## 10258 ## QUERY
SQL: SELECT `adminnotification_inbox`.* FROM `adminnotification_inbox` WHERE (title = 'Customize your order and invoice numbers to protect your sales using Order Number Customizer by XTENTO.') AND (url = 'http://www.xtento.com/magento-extensions/order-number-customizer.html?utm_source=feed&utm_medium=feed&utm_campaign=order_number_customizer')
AFF: 1
TIME: 0.0008

In case you have enabled $_logCallStack you will have also a TRACE parte

TRACE: #1 Varien_Db_Adapter_Pdo_Mysql#00000000644036ff00007f14137095e6#->_debugStat(2, 'SELECT `adminnot...', array(), &Varien_Db_Statement_Pdo_Mysql#000000006440371700007f14137095e6#) called at [lib/Varien/Db/Adapter/Pdo/Mysql.php:424]
#2 Varien_Db_Adapter_Pdo_Mysql#00000000644036ff00007f14137095e6#->query(&Varien_Db_Select#000000006440371800007f14137095e6#, array()) called at [lib/Zend/Db/Adapter/Abstract.php:753]
#3 Varien_Db_Adapter_Pdo_Mysql[Zend_Db_Adapter_Abstract]#00000000644036ff00007f14137095e6#->fetchRow(&Varien_Db_Select#000000006440371800007f14137095e6#) called at [app/code/core/Mage/AdminNotification/Model/Resource/Inbox.php:116]
#4 Mage_AdminNotification_Model_Resource_Inbox#00000000644034c500007f14137095e6#->parse(&Mage_AdminNotification_Model_Inbox#00000000644034e900007f14137095e6#, array(array('severity' => 4, 'date_added' => '2013-10-23 13:00...', 'title' => 'Customizing your...', 'description' => 'Easily add new c...', 'url' => 'http://www.xtent...'), array('severity' => 4, 'date_added' => '2014-04-09 17:00...', 'title' => 'Customize your o...', 'description' => 'Customize your M...', 'url' => 'http://www.xtent...'), array('severity' => 4, 'date_added' => '2014-05-06 17:00...', 'title' => 'Process orders i...', 'description' => 'Process all your...', 'url' => 'http://www.xtent...'))) called at [app/code/core/Mage/AdminNotification/Model/Inbox.php:118]
#5 Mage_AdminNotification_Model_Inbox#00000000644034e900007f14137095e6#->parse(array(array('severity' => 4, 'date_added' => '2013-10-23 13:00...', 'title' => 'Customizing your...', 'description' => 'Easily add new c...', 'url' => 'http://www.xtent...'), array('severity' => 4, 'date_added' => '2014-04-09 17:00...', 'title' => 'Customize your o...', 'description' => 'Customize your M...', 'url' => 'http://www.xtent...'), array('severity' => 4, 'date_added' => '2014-05-06 17:00...', 'title' => 'Process orders i...', 'description' => 'Process all your...', 'url' => 'http://www.xtent...'))) called at [app/code/local/Xtento/XtCore/Model/Feed.php:57]
#6 Xtento_XtCore_Model_Feed#000000006440348500007f14137095e6#->checkUpdate(&Varien_Event_Observer#00000000644034d900007f14137095e6#) called at [app/code/core/Mage/Core/Model/App.php:1338]
#7 Mage_Core_Model_App#00000000644036ef00007f14137095e6#->_callObserverMethod(&Xtento_XtCore_Model_Feed#000000006440348500007f14137095e6#, 'checkUpdate', &Varien_Event_Observer#00000000644034d900007f14137095e6#) called at [app/code/core/Mage/Core/Model/App.php:1317]
#8 Mage_Core_Model_App#00000000644036ef00007f14137095e6#->dispatchEvent('controller_actio...', array('controller_actio...' => &Fishpig_Wordpress_Adminhtml_WordpressController#000000006440364a00007f14137095e6#)) called at [app/Mage.php:451]
#9 Mage::dispatchEvent('controller_actio...', array('controller_actio...' => &Fishpig_Wordpress_Adminhtml_WordpressController#000000006440364a00007f14137095e6#)) called at [app/code/core/Mage/Core/Controller/Varien/Action.php:528]
#10 Fishpig_Wordpress_Adminhtml_WordpressController[Mage_Core_Controller_Varien_Action]#000000006440364a00007f14137095e6#->preDispatch() called at [app/code/core/Mage/Adminhtml/Controller/Action.php:160]
#11 Fishpig_Wordpress_Adminhtml_WordpressController[Mage_Adminhtml_Controller_Action]#000000006440364a00007f14137095e6#->preDispatch() called at [app/code/core/Mage/Core/Controller/Varien/Action.php:408]
#12 Fishpig_Wordpress_Adminhtml_WordpressController[Mage_Core_Controller_Varien_Action]#000000006440364a00007f14137095e6#->dispatch('checkVersion') called at [app/code/core/Mage/Core/Controller/Varien/Router/Standard.php:250]
#13 Mage_Core_Controller_Varien_Router_Admin[Mage_Core_Controller_Varien_Router_Standard]#00000000644036ae00007f14137095e6#->match(&Mage_Core_Controller_Request_Http#000000006440365c00007f14137095e6#) called at [app/code/core/Mage/Core/Controller/Varien/Front.php:176]
#14 Mage_Core_Controller_Varien_Front#000000006440365700007f14137095e6#->dispatch() called at [app/code/core/Mage/Core/Model/App.php:354]
#15 Mage_Core_Model_App#00000000644036ef00007f14137095e6#->run(array('scope_code' => '', 'scope_type' => 'store', 'options' => array())) called at [app/Mage.php:687]
#16 Mage::run('', 'store') called at [index.php:87]

Activate the Zend SQL Profiler with the following node in your app/etc/local.xml

<resources>
 <default_setup>
  <connection>
   <profiler>1</profiler>

Then you can access the profiler somewhere in your code and retrieve a lot of informations about all executed queries:

$profiler = Mage::getSingleton('core/resource')->getConnection('core_write')->getProfiler();

To simply output all queries:

print_r($profiler->getQueryProfiles());

You can add these two lines at the end of index.php to see all queries at the bottom of each page. Be aware that this will break AJAX requests that return a JSON response, so you might consider logging the queries instead of printing them, with this code (again, add it at the end of index.php):

$profiler = Mage::getSingleton('core/resource')->getConnection('core_write')->getProfiler();
Mage::log(print_r($profiler->getQueryProfiles(), true), null, 'queries.log', true);

Then you will find all queries in var/log/queries.log

Don't forget to remove the lines again after you finished debugging!


I'm not 100% sure this will catch every query, but most run through the query method Zend_Db_Adapter_Abstract query method in

lib/Zend/Db/Adapter/Abstract.php

With that in mind, you could temporarily add some debugging statements (to a copy you make in

app/code/local/Mage

to be safe)

public function query($sql, $bind = array())
{
    // connect to the database if needed
    $this->_connect();

    // is the $sql a Zend_Db_Select object?
    if ($sql instanceof Zend_Db_Select) {
        if (empty($bind)) {
            $bind = $sql->getBind();
        }

        $sql = $sql->assemble();
    }
    echo '$sql' . "\n<br />\n";
    var_dump($bind);