如何在Magento中打印所有查询?

时间:2010-03-24 02:14:58

标签: php mysql magento debugging

是否可以在Magento中显示所有查询字符串?我真的很想看看执行了什么查询。

由于

6 个答案:

答案 0 :(得分:53)

在Zend_Db_Adapter_Pdo_Mysql

Magento 1.4 : lib/varien/Db/Adapter/Pdo/Mysql.php

protected $_debug               = true;
protected $_logAllQueries       = true;

和(如果还没有)创建

中定义的文件夹
protected $_debugFile           = 'var/debug/sql.txt';

授予读/写权限

答案 1 :(得分:26)

我不是100%确定这会抓住每个查询,但大多数都会在

中运行查询方法Zend_Db_Adapter_Abstract query方法
lib/Zend/Db/Adapter/Abstract.php

考虑到这一点,您可以临时添加一些调试语句(对于您在app/code/local/Mage中创建的副本是安全的)

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

如果您需要全部捕获它们,那么最好在MySQL级别执行此操作(根据您的主机/ IT情况,这并不总是可行)

答案 2 :(得分:22)

使用local.xml中的以下节点激活Zend SQL事件探查器

<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

中找到所有查询

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

答案 3 :(得分:6)

根据您的活动,查询会有很大差异。如果您可以控制MySQL服务器,请尝试打开查询日志记录:

set global general_log = on

然后,您可以获取SQL日志以查看查询。作为一个词或警告,Magento倾向于在每个页面加载时执行数十个查询,并且数百个查询用于保存对象。

谢谢, 乔

答案 4 :(得分:5)

$collection->printLogQuery(true);

答案 5 :(得分:1)

打开MySQL日志记录将确保记录所有查询事务。以下是打开日志记录的方法。要打开日志记录以记录到文件。如果在Windows上,请将以下内容放在my.cnf文件或my.ini文件中,然后重新启动MySQL。

log = /path/to/your/logfile.log

然后,如果要登录表 mysql.general_log ,请运行以下查询:

SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';

如果要登录文件,请运行这些:

SET GLOBAL log_output = "FILE"; 
SET GLOBAL general_log = 'ON';
相关问题