Menu

Laravel Slow Query Detection: Catch Them Before Users Do
📰

Laravel Slow Query Detection: Catch Them Before Users Do

DEV Community: laravel·Shakil Alam·1 day ago
#1Z2yMJBR
#devtolaravel#devto#dev#query#slow
Reading 0:00
15s threshold

What You’ll Learn

Setting up DB::listen() with a configurable threshold

Logging slow queries to a dedicated query channel

Reconstructing the full SQL with bound parameters

The DB::prohibitDestructiveCommands() safety rail

Model::shouldBeStrict() for N+1 detection in development

Why Slow Queries Matter

Slow queries are the most common cause of slow pages in Laravel applications. They’re also the hardest to find post-production because:

They only appear under real load

Individual pages may seem fine until traffic increases

They compound: a 3-second query running on every pageload is invisible in development but catastrophic at 100 concurrent users

Proactive slow query logging catches them before users experience them.

DB::listen() in AppServiceProvider

Register the listener in AppServiceProvider::boot() :

// app/Providers/AppServiceProvider.php public function boot ( ) : void { $this -> registerSlowQueryDetection ( ) ; // ...…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.