Skip to content

Instantly share code, notes, and snippets.

@atishgoswami
Last active January 18, 2016 09:10
Show Gist options
  • Save atishgoswami/03ed44423ff371dd7761 to your computer and use it in GitHub Desktop.
Save atishgoswami/03ed44423ff371dd7761 to your computer and use it in GitHub Desktop.
Magento2 DB Profiler
<?php
//app/etc/env.php
//Add following to default connection config array
[
'profiler' => [
'class' => '\Magento\Framework\DB\Profiler',
'enabled' => true,
],
]
//After the index.php code add this
/** @var \Magento\Framework\App\ResourceConnection $res */
$res = \Magento\Framework\App\ObjectManager::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>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment