# Performance Optimization (Laravel) > Optymalizacja zapytań, problem N+1, eager loading, lazy loading, strategie cache'owania, indeksowanie bazy danych, narzędzia profilowania - 22 pytań z rozmów - Senior - [Pytania z rozmów: Laravel](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne.md) ## 1. Czym jest problem N+1 w Laravel? **Odpowiedź** Problem N+1 występuje, gdy Eloquent wykonuje jedno zapytanie, aby pobrać kolekcję, a następnie dodatkowe zapytanie na każdy element, aby załadować relację. Mnoży to dostępy do bazy danych i poważnie pogarsza wydajność przy dużej skali. Użycie eager loading z with() ładuje wszystkie relacje w jednym dodatkowym zapytaniu zamiast N zapytań. ## 2. Której metody należy użyć, aby rozwiązać problem N+1 podczas ładowania relacji? **Odpowiedź** Metoda with() wykonuje eager loading, który ładuje relacje w jednym dodatkowym zapytaniu. Zamiast wykonywać jedno zapytanie na model podczas dostępu do relacji, wszystkie relacje są ładowane z wyprzedzeniem. Drastycznie zmniejsza to liczbę zapytań SQL i poprawia wydajność. ## 3. Jaka jest różnica między eager loading a lazy loading? **Odpowiedź** Eager loading ładuje relacje natychmiast wraz z głównym zapytaniem za pomocą with(), podczas gdy lazy loading ładuje relacje na żądanie przy pierwszym dostępie. Lazy loading może powodować problem N+1, jeśli relacje są pobierane w pętli. Eager loading jest zalecany, gdy relacje będą używane, aby uniknąć wielu zapytań. ## Jeszcze 19 dostępnych pytań - Jaką rolę pełni Cache::remember() w Laravel? - Jaka jest różnica między Cache::remember() a Cache::rememberForever()? Zarejestruj się za darmo: https://sharpskill.dev/pl/login ## Inne tematy rekrutacyjne Laravel - [Podstawy PHP](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/php-basics.md): 25 pytań, Junior - [Podstawy programowania obiektowego w PHP](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/php-oop-essentials.md): 20 pytań, Junior - [Composer & Autoloading](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/composer-autoloading.md): 18 pytań, Junior - [Podstawy Laravel](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/laravel-fundamentals.md): 20 pytań, Junior - [Routing w Laravel](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/laravel-routing.md): 20 pytań, Junior - [Blade Templates](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/blade-templates.md): 18 pytań, Junior - [Request & Response](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/request-response.md): 20 pytań, Junior - [Podstawy Eloquent ORM](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/eloquent-orm-basics.md): 22 pytań, Junior - [Eloquent Relationships](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/eloquent-relationships.md): 25 pytań, Mid-Level - [Migrations & Schema Builder](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/database-migrations.md): 20 pytań, Mid-Level - [Walidacja i formularze](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/validation-forms.md): 22 pytań, Mid-Level - [Uwierzytelnianie](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/authentication.md): 20 pytań, Mid-Level - [Autoryzacja i Policies](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/authorization-policies.md): 18 pytań, Mid-Level - [API Resources & Authentication](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/api-resources-authentication.md): 26 pytań, Mid-Level - [Middleware](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/middleware.md): 18 pytań, Mid-Level - [Service Container & DI](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/service-container-di.md): 20 pytań, Mid-Level - [Queues & Jobs](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/queues-jobs.md): 22 pytań, Mid-Level - [Events & Listeners](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/events-listeners.md): 18 pytań, Mid-Level - [Powiadomienia & Mail](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/notifications-mail.md): 20 pytań, Mid-Level - [File Storage](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/file-storage.md): 18 pytań, Mid-Level - [Testing & PHPUnit](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/testing-phpunit.md): 24 pytań, Mid-Level - [Caching](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/caching.md): 18 pytań, Mid-Level - [Livewire & Inertia](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/livewire-inertia.md): 20 pytań, Mid-Level - [Eloquent Advanced](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/eloquent-advanced.md): 24 pytań, Senior - [Repository Pattern](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/repository-pattern.md): 20 pytań, Senior - [Laravel Packages](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/laravel-packages.md): 20 pytań, Senior - [Security Best Practices](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/security-best-practices.md): 22 pytań, Senior - [Laravel Octane](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/laravel-octane.md): 18 pytań, Senior - [Laravel Distributed Systems](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/laravel-distributed-systems.md): 22 pytań, Senior - [Observability & Monitoring](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/observability-monitoring.md): 20 pytań, Senior - [Deployment & DevOps](https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/deployment-devops.md): 20 pytań, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/pl/technologies/laravel/pytania-rekrutacyjne/performance-optimization