Skip to content

Commit

Permalink
Add EXPLAIN to QueryCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Oct 10, 2014
1 parent 9c74216 commit c29988e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/DataCollector/QueryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class QueryCollector extends PDOCollector
protected $queries = array();
protected $renderSqlWithParams = false;
protected $findSource = false;
protected $explainQuery = false;
protected $explainTypes = array('SELECT'); // array('SELECT', 'INSERT', 'UPDATE', 'DELETE'); for MySQL 5.6.3+

/**
* @param TimeDataCollector $timeCollector
Expand Down Expand Up @@ -53,12 +55,21 @@ public function setFindSource($value = true)
*/
public function addQuery($query, $bindings, $time, $connection)
{
$explainResults = array();
$time = $time / 1000;
$endTime = microtime(true);
$startTime = $endTime - $time;

$pdo = $connection->getPdo();
$bindings = $connection->prepareBindings($bindings);

// Run EXPLAIN on this query (if needed)
if ($this->explainQuery && preg_match('/^('.implode($this->explainTypes).') /i', $query)) {
$statement = $pdo->prepare('EXPLAIN ' . $query);
$statement->execute($bindings);
$explainResults = $statement->fetchAll(\PDO::FETCH_ASSOC);
}

$bindings = $this->checkBindings($bindings);
if (!empty($bindings) && $this->renderSqlWithParams) {
foreach ($bindings as $binding) {
Expand All @@ -81,6 +92,16 @@ public function addQuery($query, $bindings, $time, $connection)
'source' => $source,
);

//Add the results from the explain as new rows
foreach($explainResults as $result){
$this->queries[] = array(
'query' => 'EXPLAIN ' . $query,
'bindings' => $result,
'time' => $time,
'source' => $source,
);
}

if ($this->timeCollector !== null) {
$this->timeCollector->addMeasure($query, $startTime, $endTime);
}
Expand Down

0 comments on commit c29988e

Please sign in to comment.