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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2504931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:43:49  来源:igfitidea点击:

How do I print all the queries in Magento?

phpmysqlmagentodebugging

提问by Moon

Is it possible to display all the query strings in Magento? I really like to see what queries are executed.

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

Thanks

谢谢

回答by Claudia Schasiepen

In Varien_Db_Adapter_Pdo_Mysql

在 Varien_Db_Adapter_Pdo_Mysql 中

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

set

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

and (if nor already there) create the folder defined in

和(如果也没有)创建定义的文件夹

protected $_debugFile           = 'var/debug/sql.txt';

Give read / write permission

授予读/写权限

回答by Alan Storm

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

我不是 100% 确定这会捕获每个查询,但大多数运行通过查询方法Zend_Db_Adapter_Abstractquery方法

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/Mageto be safe)

考虑到这一点,您可以临时添加一些调试语句(为了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);

If you need to catch them all, you'd be better off doing this at the MySQL level (which isn't always possible depending on your host/IT situation)

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

回答by Fabian Schmengler

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

使用 local.xml 中的以下节点激活 Zend SQL Profiler

<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.phpto 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):

您可以在末尾添加这两行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);

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

然后你会发现所有的查询 var/log/queries.log

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

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

回答by Joseph Mastey

The queries will vary significantly depending on your activities. If you have some control over your MySQL server, try turning on query logging:

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

set global general_log = on

Then you can get the SQL log to see queries. As a word or warning, Magento tends to execute dozens of queries on every page load, and hundreds to save an object.

然后就可以拿到SQL日志来查看查询了。作为一句话或警告,Magento 倾向于在每次页面加载时执行数十个查询,并执行数百个查询以保存一个对象。

Thanks, Joe

谢谢,乔

回答by Mukesh Chapagain

$collection->printLogQuery(true);

回答by s-hunter

Turn on the MySQL logging will sure log all queries transactions. Here is how you can turn on the logging on.To turn on the logging to log to a file. Place the following in the my.cnf file or my.ini file if on windows, and restart MySQL.

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

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

Then if you want to log to the table mysql.general_log, run these queries:

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

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

Run these if you want to log to the file:

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

SET GLOBAL log_output = "FILE"; 
SET GLOBAL general_log = 'ON';