Kritim Yantra
Jun 28, 2025
Ever felt like your Laravel app is running slower than a snail on vacation? ππ¨ The culprit? Inefficient database queries!
Debugging and optimizing SQL queries in Laravel 12 can dramatically speed up your app and make debugging a breeze. Whether you're a beginner or a seasoned dev, this guide will help you find, fix, and optimize those pesky slow queries.
Before diving in, letβs understand why this matters:
β
Faster App Performance β Slow queries = slow app. Optimized queries = happy users.
β
Lower Server Costs β Efficient queries use fewer server resources.
β
Easier Debugging β Quickly find bottlenecks in your code.
Laravel provides built-in tools to help you see whatβs happening behind the scenes.
First, ensure debugging is enabled in .env
:
APP_DEBUG=true
This will show detailed error logs when something goes wrong.
Want to see every query running? Add this to AppServiceProvider.php
:
public function boot()
{
if (env('APP_DEBUG')) {
\DB::listen(function ($query) {
\Log::info([
'query' => $query->sql,
'bindings' => $query->bindings,
'time' => $query->time
]);
});
}
}
Now check storage/logs/laravel.log
for all executed queries!
Telescope is Laravelβs debug dashboard. Install it:
composer require laravel/telescope
php artisan telescope:install
php artisan migrate
Visit /telescope
to see:
β Executed Queries
β Request/Response Data
β Performance Metrics
Now that weβve found slow queries, letβs optimize them!
β Bad Way (N+1 Queries)
$users = User::all();
foreach ($users as $user) {
echo $user->posts->count(); // New query per user!
}
β Good Way (1 Query with Eager Loading)
$users = User::with('posts')->get();
foreach ($users as $user) {
echo $user->posts->count(); // No extra queries!
}
Instead of fetching all columns:
User::all(); // SELECT * FROM users
Fetch only what you need:
User::select('id', 'name')->get(); // SELECT id, name FROM users
Indexes speed up searches. Add them in migrations:
Schema::table('users', function (Blueprint $table) {
$table->index('email'); // Faster email searches!
});
Why run the same query repeatedly? Cache it!
$users = Cache::remember('active_users', 3600, function () {
return User::where('active', 1)->get();
});
Now, the query runs only once per hour!
Install this handy package:
composer require barryvdh/laravel-debugbar --dev
It shows:
β Query Execution Time
β Memory Usage
β Duplicate Queries
Enable MySQLβs slow query log in my.cnf
:
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 2
Now, MySQL logs queries taking longer than 2 seconds.
β Enable APP_DEBUG
& log queries to find performance issues.
β Use Eager Loading (with()
) to avoid N+1 problems.
β Select only necessary columns to reduce data load.
β Add indexes to frequently searched columns.
β Cache results for repeated queries.
β Use Laravel Telescope & Debugbar for real-time monitoring.
Optimizing SQL queries in Laravel 12 boosts speed, saves resources, and improves user experience. Start debugging today, and watch your app fly! π
Got questions? Drop them in the comments! π¬π
Happy coding! π»π₯
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google