パフォーマンス最適化
Laravelパフォーマンス最適化
Section titled “Laravelパフォーマンス最適化”キャッシング
Section titled “キャッシング”<?php// 設定のキャッシュphp artisan config:cache
// ルートのキャッシュphp artisan route:cache
// ビューのキャッシュphp artisan view:cache
// すべてのキャッシュphp artisan optimizeクエリの最適化
Section titled “クエリの最適化”<?php// Eager Loading(N+1問題の解決)$users = User::with('posts')->get();
// 特定のカラムのみ取得$users = User::select('id', 'name', 'email')->get();
// チャンク処理User::chunk(100, function ($users) { foreach ($users as $user) { // 処理 }});データベースインデックス
Section titled “データベースインデックス”<?php// マイグレーションでインデックスを追加public function up(){ Schema::table('users', function (Blueprint $table) { $table->index('email'); $table->index(['name', 'email']); // 複合インデックス });}実践例: キャッシュの活用
Section titled “実践例: キャッシュの活用”<?php// キャッシュを使用$users = Cache::remember('users', 3600, function () { return User::all();});
// キャッシュの削除Cache::forget('users');