如何记录所有Magento SQL?


23

有没有简单的方法来记录Magento执行的所有查询?

目标是打印所有执行的SQL,例如在页面加载期间

Answers:


43

如果要确保所有SQL都已实际记录,建议您使用第三方软件:

  • Neon Profile SQL实际上是免费的并且可以正常工作
    (您需要编辑local.xml才能将Magento附加到Neon上……它像记录所有通过它的SQL代理一样工作)

另外,本机的Magento / Varien方法可以是以下一种方法:

  1. 编辑 lib/Varien/Db/Adapter/Pdo/Mysql.php
  2. 将以下属性更改为true(第103行)
  3. 您将在此处创建一个日志文件 var/debug/pdo_mysql.log

这里要更改的行:(注释不言自明)

 /**
     * 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;

生成的日志如下所示:

## 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

如果您已启用$_logCallStack,则还将有一个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]

15

在您的以下节点中激活Zend SQL Profiler app/etc/local.xml

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

然后,您可以在代码中的某个位置访问事件探查器,并检索有关所有已执行查询的许多信息:

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

要简单地输出所有查询:

print_r($profiler->getQueryProfiles());

您可以在这两行末尾添加这两行,index.php以在每页底部查看所有查询。请注意,这会破坏返回JSON响应的AJAX请求,因此您可以考虑使用以下代码记录查询而不是打印查询(再次在末尾添加index.php):

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

然后,您将在中找到所有查询 var/log/queries.log

完成调试后,不要忘记再次删除这些行!


2

我不是100%肯定会捕获每个查询,但是大多数查询都是通过查询方法Zend_Db_Adapter_Abstract查询来运行的

lib / Zend / Db / Adapter / Abstract.php

考虑到这一点,您可以临时添加一些调试语句(在您制作的副本中

应用/代码/本地/法师

为了安全)

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);

2
我想我曾经使用过类似的方法,但是我发现我建议的方法更好:)
2014年

“暂时添加一些调试语句是安全的”。这种方法没有安全的方法。
凯文·施罗德

2

如果您有权访问mysql服务器,并且没有其他运行查询的项目,则可以尝试从mysql添加常规日志。在这里查看如何执行此操作/programming/6479107/how-to-enable-mysql-query-log, 因为它会将所有查询写入日志文件,而且很快就会变大,因此请务必打开完成后关闭它。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.