Laravel Performance Optimization: Hız ve Verimlilik Rehberi
z
zafer ak
Yazar
27 November 2025
2 dakika okuma
2 görüntülenme
Laravel uygulamalarında performans optimizasyonu. Database queries, caching, eager loading ve profiling teknikleri.
Performance Neden Önemli?
- User experience (3s+ = %53 bounce)
- SEO ranking faktörü
- Server maliyeti
- Conversion rate
Database Optimization
N+1 Query Problemi
// KÖTÜ - N+1 query
$posts = Post::all();
foreach ($posts as $post) {
echo $post->author->name; // Her post için query
}
// İYİ - Eager loading
$posts = Post::with('author')->get();
foreach ($posts as $post) {
echo $post->author->name; // Tek query
}
Lazy Loading Disabled
// AppServiceProvider
public function boot()
{
Model::preventLazyLoading(!app()->isProduction());
}
Query Optimization
// Select sadece gerekli kolonlar
User::select('id', 'name', 'email')->get();
// Chunking büyük data için
User::chunk(1000, function ($users) {
foreach ($users as $user) {
// Process
}
});
// Cursor (memory efficient)
foreach (User::cursor() as $user) {
// Process
}
// Index kullanımı
Schema::table('posts', function (Blueprint $table) {
$table->index('user_id');
$table->index(['status', 'published_at']);
});
Caching Stratejileri
// Query caching
$users = Cache::remember('active_users', 3600, function () {
return User::where('active', true)->get();
});
// Route caching (production)
php artisan route:cache
// Config caching
php artisan config:cache
// View caching
php artisan view:cache
// Event caching
php artisan event:cache
Queue Kullanımı
// Email gönderimini queue'a at
Mail::to($user)->queue(new WelcomeMail($user));
// Job dispatch
ProcessPodcast::dispatch($podcast);
// Batch processing
Bus::batch([
new ImportCsv($file1),
new ImportCsv($file2),
])->dispatch();
API Response Optimization
// API Resource
class UserResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'posts_count' => $this->whenCounted('posts'),
'profile' => new ProfileResource($this->whenLoaded('profile')),
];
}
}
// Pagination
return UserResource::collection(
User::paginate(15)
);
Frontend Optimization
// Vite production build
npm run build
// Asset versioning
@vite(['resources/css/app.css', 'resources/js/app.js'])
// Image optimization
 }})
Profiling Tools
Laravel Debugbar
composer require barryvdh/laravel-debugbar --dev
// Queries, views, route, memory görüntüleme
Laravel Telescope
composer require laravel/telescope --dev
php artisan telescope:install
// Requests, exceptions, logs, queries takibi
Database Queries
DB::enableQueryLog();
// ... queries
dd(DB::getQueryLog());
Production Checklist
✅ APP_DEBUG=false
✅ php artisan config:cache
✅ php artisan route:cache
✅ php artisan view:cache
✅ php artisan optimize
✅ Composer autoload: composer dump-autoload -o
✅ OPcache enabled
✅ Redis for cache/session
✅ CDN for static assets
✅ Database indexes
Sonuç
Performance optimization sürekli bir süreçtir. Profiling yapın, bottleneck'leri tespit edin ve iteratif olarak iyileştirin.