Laravel Database

Log and Audit Slow Database Queries with DB::listen()

Punyapal Shah 1 min read
edit this tip
Use DB::listen() in AppServiceProvider to monitor executed queries, log slow operations, and inspect raw SQL bindings during local development.

Debugging database performance bottlenecks requires visibility into every executed query, its execution time, and bound parameters.

Laravel provides DB::listen() to register a callback executed after every database query.

Logging Slow Queries in Development

Add the listener inside AppServiceProvider::boot():

namespace AppProviders;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use IlluminateSupportServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        DB::listen(function ($query) {
            // Log any query that takes longer than 100ms
            if ($query->time > 100) {
                Log::warning("Slow Query [{$query->time}ms]: {$query->sql}", [
                    'bindings'   => $query->bindings,
                    'connection' => $query->connectionName,
                ]);
            }
        });
    }
}

Inspecting Query Properties

  • $query->sql: The prepared SQL statement (e.g. select * from users where email = ?).
  • $query->bindings: Array of bound parameters.
  • $query->time: Execution time in milliseconds.
  • $query->connectionName: Database connection identifier (e.g. mysql, pgsql).

Summary

  • Intercepts all database queries application-wide.
  • Enables threshold-based slow query logging.
  • Essential tool for identifying missing indexes and unoptimized joins during development.
Tags: Laravel Database Performance Debugging
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips