Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions database/migrations/2023_06_07_000001_create_pulse_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ public function up(): void
Schema::create('pulse_slow_queries', function (Blueprint $table) {
$table->datetime('date');
$table->string('user_id')->nullable();
$table->string('sql');
$table->text('sql');
$table->char('sql_hash', 16)->charset('binary')->virtualAs('UNHEX(MD5(`sql`))');
$table->unsignedInteger('duration');

$table->index(['date', 'sql', 'duration']); // slow_queries
$table->index(['date', 'sql_hash', 'duration']); // slow_queries
});

Schema::create('pulse_jobs', function (Blueprint $table) {
Expand All @@ -87,10 +88,11 @@ public function up(): void

Schema::create('pulse_outgoing_requests', function (Blueprint $table) {
$table->datetime('date');
$table->string('uri');
$table->text('uri');
$table->char('uri_hash', 16)->charset('binary')->virtualAs('UNHEX(MD5(`uri`))');
$table->unsignedInteger('duration');
$table->string('user_id')->nullable();
$table->index(['uri', 'date', 'duration']);
$table->index(['uri_hash', 'date', 'duration']);
});

Schema::create('pulse_queue_sizes', function (Blueprint $table) {
Expand Down
4 changes: 2 additions & 2 deletions src/Queries/SlowOutgoingRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function __invoke(Interval $interval): Collection
$now = new CarbonImmutable;

return $this->connection()->table('pulse_outgoing_requests')
->selectRaw('`uri`, COUNT(*) as count, MAX(duration) AS slowest')
->selectRaw('MAX(uri) AS uri, COUNT(*) AS count, MAX(duration) AS slowest')
->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('duration', '>=', $this->config->get('pulse.recorders.'.OutgoingRequests::class.'.threshold'))
->groupBy('uri')
->groupBy('uri_hash')
->orderByDesc('slowest')
->limit(101)
->get();
Expand Down
4 changes: 2 additions & 2 deletions src/Queries/SlowQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function __invoke(Interval $interval): Collection
$now = new CarbonImmutable;

return $this->connection()->table('pulse_slow_queries')
->selectRaw('`sql`, COUNT(*) as count, MAX(duration) AS slowest')
->selectRaw('MAX(`sql`) AS `sql`, COUNT(*) AS count, MAX(duration) AS slowest')
->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->groupBy('sql')
->groupBy('sql_hash')
->orderByDesc('slowest')
->get();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/SlowQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
expect(Pulse::entries())->toHaveCount(0);
$queries = Pulse::ignore(fn () => DB::table('pulse_slow_queries')->get());
expect($queries)->toHaveCount(1);
expect((array) $queries[0])->toEqual([
expect($queries[0])->toHaveProperties([
'date' => '2000-01-02 03:04:00',
'user_id' => null,
'sql' => 'select * from users',
Expand Down