diff --git a/database/migrations/2023_06_07_000001_create_pulse_tables.php b/database/migrations/2023_06_07_000001_create_pulse_tables.php index df6a984c..d04fb375 100644 --- a/database/migrations/2023_06_07_000001_create_pulse_tables.php +++ b/database/migrations/2023_06_07_000001_create_pulse_tables.php @@ -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) { @@ -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) { diff --git a/src/Queries/SlowOutgoingRequests.php b/src/Queries/SlowOutgoingRequests.php index 815feff7..45e8eb14 100644 --- a/src/Queries/SlowOutgoingRequests.php +++ b/src/Queries/SlowOutgoingRequests.php @@ -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(); diff --git a/src/Queries/SlowQueries.php b/src/Queries/SlowQueries.php index 74d958a2..ca5db398 100644 --- a/src/Queries/SlowQueries.php +++ b/src/Queries/SlowQueries.php @@ -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(); } diff --git a/tests/Feature/SlowQueriesTest.php b/tests/Feature/SlowQueriesTest.php index 34aac04a..15a0aa05 100644 --- a/tests/Feature/SlowQueriesTest.php +++ b/tests/Feature/SlowQueriesTest.php @@ -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',