Magento数据库分析器显示页面上执行的所有查询,包括每个查询的时间和什么样的参数被执行。
修改<Magento 2 安装目录>/app/etc/env.php 将下列代码添加到 magento2/blob/2.0/lib/internal/Magento/Framework/DB/Profiler.php:
<Magento 2 安装目录>/app/etc/env.php
'profiler' => [ 'class' => '\Magento\Framework\DB\Profiler', 'enabled' => true, ],
示例如下:
'db' => array ( 'table_prefix' => '', 'connection' => array ( 'default' => array ( 'host' => 'localhost', 'dbname' => 'magento', 'username' => 'magento', 'password' => 'magento', 'model' => 'mysql4', 'engine' => 'innodb', 'initStatements' => 'SET NAMES utf8;', 'active' => '1', 'profiler' => [ 'class' => '\Magento\Framework\DB\Profiler', 'enabled' => true, ], ), ), ),
/** @var \Magento\Framework\App\ResourceConnection $res */ $res = \Magento\Framework\AppObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection'); /** @var Magento\Framework\DB\Profiler $profiler */ $profiler = $res->getConnection('read')->getProfiler(); echo "<table cellpadding='0' cellspacing='0' border='1'>"; echo "<tr>"; echo "<th>Time <br/>[Total Time: ".$profiler->getTotalElapsedSecs()." secs]</th>"; echo "<th>SQL [Total: ".$profiler->getTotalNumQueries()." queries]</th>"; echo "<th>Query Params</th>"; echo "</tr>"; foreach ($profiler->getQueryProfiles() as $query) { /** @var Zend_Db_Profiler_Query $query*/ echo '<tr>'; echo '<td>', number_format(1000 * $query->getElapsedSecs(), 2), 'ms', '</td>'; echo '<td>', $query->getQuery(), '</td>'; echo '<td>', json_encode($query->getQueryParams()), '</td>'; echo '</tr>'; } echo "</table>";
访问magento 2 的任何页面,示例结果如下图: