Struggling with slow performance in your Laravel application? You’re not alone.
One of the most common causes of sluggish apps is slow database queries — and identifying them early can save you hours of debugging, user complaints, and lost traffic. Fortunately, Laravel makes it easy to log slow queries with just a few lines of code.
In this post, I’ll walk you through a step-by-step approach to detect and log slow queries in Laravel — even if you’re just practicing on dummy data.
🧠 Why Should You Track Slow Queries?
Before diving into code, let’s understand why this matters:
- 🐢 Slow queries slow down your entire application
- 🧩 They can indicate missing indexes, poor query structure, or unnecessary joins
- 💰 In a live app, they can cost you users — and revenue
Being able to track and log these queries is a powerful way to take control of performance.
🛠️ Step 1: Enable Query Logging in Laravel
In AppServiceProvider.php
, add the following to the boot()
method:
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
public function boot()
{
DB::enableQueryLog();
DB::whenQueryingForLongerThan(1000, function ($connection) {
Log::warning('⚠️ Long running queries detected:', $connection->getQueryLog());
});
}
🕐 This logs any query that takes longer than 1000ms (1 second).
You can adjust the threshold as needed — even down to 50ms for dev environments.
🧪 Step 2: Create Dummy Data for Testing
Use a seeder to simulate a real-world dataset:
php artisan make:seeder PostSeeder
In PostSeeder.php
:
use App\Models\Post;
public function run()
{
Post::factory()->count(10000)->create();
}
Run the seeder:
php artisan db:seed --class=PostSeeder
Now your database has enough records to test heavy queries.
⚡ Step 3: Trigger a Slow Query (For Testing)
You can simulate a slow query using a simple route:
use Illuminate\Support\Facades\DB;
Route::get('/slow-query', function () {
usleep(2000000); // 2 seconds delay (in microseconds)
return DB::table('posts')->limit(1000)->get();
});
Or if you’re using MySQL, try:
return DB::select('SELECT SLEEP(2), title FROM posts LIMIT 1');
This will create a delay long enough to trigger the logging.
📂 Step 4: Check the Logs
Open this file:
storage/logs/laravel.log
And you’ll see something like:
[2025-04-04 14:15:22] local.WARNING: ⚠️ Long running queries detected: [...]
Boom! You’ve just caught a slow query.
🔐 Bonus: Log Full SQL with Bindings
Want to see exact SQL statements?
DB::listen(function ($query) {
Log::info("SQL: {$query->sql} | Time: {$query->time}ms", $query->bindings);
});
Add this to your AppServiceProvider@boot()
during development.
🧩 Real-World Use Cases
- Monitoring API endpoints that return too slowly
- Debugging admin dashboards with large datasets
- Detecting missing indexes on frequently-used tables
✅ Wrap-Up
Laravel gives you the tools — you just need to flip the switch.
By logging long-running queries:
- You gain better visibility over your app’s performance
- You prevent performance bottlenecks early
- You build more efficient, scalable applications
🔔 Next step: Try this on your current project and keep the logs running while developing. You’ll be amazed how quickly you spot patterns.