Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add explanation of queries #567

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Clockwork/DataSource/EloquentDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ class EloquentDataSource extends DataSource
// Enable duplicate queries detection
protected $detectDuplicateQueries = false;

// Enable explanation of queries
protected $explain = false;

// Model name to associate with the next executed query, used to map queries to models
public $nextQueryModel;

// Create a new data source instance, takes a database manager, an event dispatcher as arguments and additional
// options as arguments
public function __construct(ConnectionResolverInterface $databaseManager, EventDispatcher $eventDispatcher, $collectQueries = true, $slowThreshold = null, $slowOnly = false, $detectDuplicateQueries = false, $collectModelsActions = true, $collectModelsRetrieved = false)
public function __construct(ConnectionResolverInterface $databaseManager, EventDispatcher $eventDispatcher, $collectQueries = true, $slowThreshold = null, $slowOnly = false, $detectDuplicateQueries = false, $collectModelsActions = true, $collectModelsRetrieved = false, $explain = false)
{
$this->databaseManager = $databaseManager;
$this->eventDispatcher = $eventDispatcher;
Expand All @@ -63,6 +66,7 @@ public function __construct(ConnectionResolverInterface $databaseManager, EventD
$this->detectDuplicateQueries = $detectDuplicateQueries;
$this->collectModelsActions = $collectModelsActions;
$this->collectModelsRetrieved = $collectModelsRetrieved;
$this->explain = $explain;

if ($slowOnly) $this->addFilter(function ($query) { return $query['duration'] > $this->slowThreshold; });
}
Expand Down Expand Up @@ -166,7 +170,8 @@ protected function registerQuery($event)
'time' => microtime(true) - $event->time / 1000,
'trace' => (new Serializer)->trace($trace),
'model' => $this->nextQueryModel,
'tags' => $this->slowThreshold !== null && $event->time > $this->slowThreshold ? [ 'slow' ] : []
'tags' => $this->slowThreshold !== null && $event->time > $this->slowThreshold ? [ 'slow' ] : [],
'explanation'=> $this->explain && str_starts_with($event->sql, 'SELECT') ? array_map(fn ($row) => $row->{'QUERY PLAN'}, $this->databaseManager->connection($event->connectionName)->select('EXPLAIN ANALYZE ' . $event->sql, $event->bindings)) : null
];

$this->nextQueryModel = null;
Expand Down