From ebc34cab0ec91870af9ce4d754f4921cf7fe4aa8 Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Fri, 25 Feb 2022 16:56:56 +0100 Subject: [PATCH 1/2] feat: add explanation of queries --- Clockwork/DataSource/EloquentDataSource.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Clockwork/DataSource/EloquentDataSource.php b/Clockwork/DataSource/EloquentDataSource.php index 8c7ab4b1..19156902 100644 --- a/Clockwork/DataSource/EloquentDataSource.php +++ b/Clockwork/DataSource/EloquentDataSource.php @@ -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; @@ -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; }); } @@ -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, 'EXPLAIN') ? array_map(fn ($row) => $row->{'QUERY PLAN'}, $this->databaseManager->connection($event->connectionName)->select('EXPLAIN ' . $event->sql, $event->bindings)) : null ]; $this->nextQueryModel = null; From a35fd7805e597e5419456e20f4ce345a630b6a85 Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Sun, 27 Feb 2022 16:47:41 +0100 Subject: [PATCH 2/2] fix: consider SELECT only --- Clockwork/DataSource/EloquentDataSource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Clockwork/DataSource/EloquentDataSource.php b/Clockwork/DataSource/EloquentDataSource.php index 19156902..0243390b 100644 --- a/Clockwork/DataSource/EloquentDataSource.php +++ b/Clockwork/DataSource/EloquentDataSource.php @@ -171,7 +171,7 @@ protected function registerQuery($event) 'trace' => (new Serializer)->trace($trace), 'model' => $this->nextQueryModel, 'tags' => $this->slowThreshold !== null && $event->time > $this->slowThreshold ? [ 'slow' ] : [], - 'explanation'=> $this->explain && !str_starts_with($event->sql, 'EXPLAIN') ? array_map(fn ($row) => $row->{'QUERY PLAN'}, $this->databaseManager->connection($event->connectionName)->select('EXPLAIN ' . $event->sql, $event->bindings)) : null + '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;