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

Feature - Mark duplicate queries #5185

Merged
merged 8 commits into from
Oct 9, 2021
36 changes: 27 additions & 9 deletions system/Debug/Toolbar/Collectors/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Database extends BaseCollector
* The query instances that have been collected
* through the DBQuery Event.
*
* @var Query[]
* @var array
*/
protected static $queries = [];

Expand All @@ -83,7 +83,19 @@ public static function collect(Query $query)
$max = $config->maxQueries ?: 100;

if (count(static::$queries) < $max) {
static::$queries[] = $query;
if (! in_array($query->getQuery(), array_column(static::$queries, 'string', null), true)) {
static::$queries[] = [
'query' => $query,
'string' => $query->getQuery(),
'duplicate' => false,
];
} else {
static::$queries[] = [
'query' => $query,
'string' => $query->getQuery(),
'duplicate' => true,
];
}
danielTiringer marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -110,8 +122,8 @@ protected function formatTimelineData(): array
$data[] = [
'name' => 'Query',
'component' => 'Database',
'start' => $query->getStartTime(true),
'duration' => $query->getDuration(),
'start' => $query['query']->getStartTime(true),
'duration' => $query['query']->getDuration(),
];
}

Expand All @@ -123,10 +135,11 @@ protected function formatTimelineData(): array
*/
public function display(): array
{
$data['queries'] = array_map(static function (Query $query) {
$data['queries'] = array_map(static function (array $query) {
return [
'duration' => ((float) $query->getDuration(5) * 1000) . ' ms',
'sql' => $query->debugToolbarDisplay(),
'class' => isset($query['duplicate']) ? 'duplicate' : '',
danielTiringer marked this conversation as resolved.
Show resolved Hide resolved
'duration' => ((float) $query['query']->getDuration(5) * 1000) . ' ms',
'sql' => $query['query']->debugToolbarDisplay(),
];
}, static::$queries);

Expand All @@ -150,13 +163,18 @@ public function getTitleDetails(): string
{
$this->getConnections();

$queryCount = count(static::$queries);
$queryCount = count(static::$queries);
$uniqueCount = count(array_filter(static::$queries, static function ($query) {
return $query['duplicate'] === false;
}));
$connectionCount = count($this->connections);

return sprintf(
'(%d Quer%s across %d Connection%s)',
'(%d total Quer%s, %d %s unique across %d Connection%s)',
$queryCount,
$queryCount > 1 ? 'ies' : 'y',
$uniqueCount,
$uniqueCount > 1 ? 'of them' : '',
$connectionCount,
$connectionCount > 1 ? 's' : ''
);
Expand Down