Laravel / PHP PHP Free

Log Slow Database Queries in Laravel (Find the Slow Ones Automatically)

Find slow queries in Laravel without guessing — log every query over 500 ms with its SQL, bindings and time.

23 lines · Updated Nov 26, 2026

Preview of Log Slow Database Queries in Laravel (Find the Slow Ones Automatically)
laravel-log-slow-database-queries.php
namespace App\Providers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        if (! config('app.debug')) {
            return;
        }
        DB::listen(function ($query) {
            if ($query->time < 500) {
                return;
            }
            Log::warning('Slow query detected', [
                'sql'      => $query->sql,
                'bindings' => $query->bindings,
                'time_ms'  => $query->time,
            ]);
        });
    }
}

About this snippet

"The dashboard is slow" is the most common complaint in any Laravel application, and the cause is almost always one or two database queries. Instead of guessing, this snippet listens to every query Laravel runs and writes the slow ones — with their SQL, bindings and execution time — to your log. Leave it running in production and the slow queries find you.

How it works

  • Laravel fires an event after every database query. The snippet registers a listener for that event in a service provider.
  • When a query takes longer than your threshold (for example 500 ms), it logs the SQL, the bound values and the time in milliseconds.
  • Fast queries are ignored, so the overhead is a single comparison per query.

How to use it

  1. Add the code to the boot() method of AppServiceProvider.
  2. Use the daily log channel so log files rotate.
  3. Browse the slow pages, then check storage/logs for "slow query" entries.

What to do with a slow query

  • Run it in MySQL with EXPLAIN. A full table scan (type: ALL) usually means a missing index on a WHERE, JOIN or ORDER BY column.
  • If the same query appears dozens of times per request, you have an N+1 problem — eager-load the relationship with with().
  • For large result sets, paginate or select only the columns you need.

Common problems

  • Nothing is logged: your threshold is higher than your slowest query, or config is cached — run php artisan optimize:clear.
  • Privacy: bindings may contain emails or other personal data. Keep the log private.

FAQ

Is it safe in production? Yes — the cost is tiny, and production is where real slow queries appear.

Related: System design basics · Deploy Laravel on a VPS

How to install

Open app/Providers/AppServiceProvider.php and put the code inside the boot() method. It only runs when APP_DEBUG is true, so your production logs stay clean — turn debug on in your local .env (APP_DEBUG=true), load the slow page, then read the log with php artisan pail or by opening storage/logs/laravel.log and searching for "Slow query detected". Works with Laravel 11, 12 and 13 on PHP 8.2 and newer.

Share this snippet

See all
Chat with us