Laravel

Performance Optimization

Query optimization, N+1 problem, eager loading, lazy loading, caching strategies, database indexing, profiling tools

22 câu hỏi phỏng vấn·
Senior
1

What is the N+1 problem in Laravel?

Câu trả lời

The N+1 problem occurs when Eloquent executes one query to retrieve a collection, then an additional query per item to load a relationship. This multiplies database accesses and severely degrades performance at scale. Using eager loading with with() loads all relationships in a single additional query instead of N queries.

2

Which method should be used to solve the N+1 problem when loading relationships?

Câu trả lời

The with() method performs eager loading which loads relationships in a single additional query. Rather than executing one query per model when accessing the relationship, all relationships are loaded upfront. This drastically reduces the number of SQL queries and improves performance.

3

What is the difference between eager loading and lazy loading?

Câu trả lời

Eager loading loads relationships immediately with the main query via with(), while lazy loading loads relationships on demand upon first access. Lazy loading can cause the N+1 problem if relationships are accessed in a loop. Eager loading is recommended when relationships will be used to avoid multiple queries.

4

What is the role of Cache::remember() in Laravel?

5

What is the difference between Cache::remember() and Cache::rememberForever()?

+19 câu hỏi phỏng vấn

Nắm vững Laravel cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí