パフォーマンス最適化
PHPパフォーマンス最適化
Section titled “PHPパフォーマンス最適化”OPcacheの有効化
Section titled “OPcacheの有効化”; php.iniopcache.enable=1opcache.memory_consumption=128opcache.max_accelerated_files=10000opcache.validate_timestamps=0 # 本番環境メモリ使用量の最適化
Section titled “メモリ使用量の最適化”<?php// 大きな配列の処理function processLargeArray($items) { foreach ($items as $item) { // 処理 yield $item; // ジェネレータを使用 }}
// 使用例foreach (processLargeArray($largeArray) as $item) { // メモリ効率的な処理}データベースクエリの最適化
Section titled “データベースクエリの最適化”<?php// N+1問題の解決// 悪い例$users = User::all();foreach ($users as $user) { $posts = $user->posts; // 各ユーザーごとにクエリ}
// 良い例(Eager Loading)$users = User::with('posts')->get();foreach ($users as $user) { $posts = $user->posts; // 既にロード済み}