Sling Academy
Home/PHP/How to optimize Laravel Eloquent queries (with examples)

How to optimize Laravel Eloquent queries (with examples)

Last updated: January 17, 2024

Introduction

Laravel’s Eloquent ORM is a simple yet powerful tool to interact with the database. However, without proper query optimization, you may run into performance issues as your application scales. In this tutorial, we’ll explore how to optimize Eloquent queries effectively.

Basics of Eloquent Optimization

Selecting Specific Columns

$users = User::select('id', 'name')->get();

By specifying the column names, you reduce the amount of data retrieved from the database, which can speed up the query execution.

Eager Loading Relationships

$books = Book::with('author')->get();

This prevents the N+1 query issue by loading related models in advance.

Intermediate Tips

Indexed Columns

Create indexes on columns that are frequently queried to enhance performance.

Where Exists Clauses

$users = User::whereHas('posts', function ($query) {
    $query->where('published', true);
})->get();

This limits the result set to users with published posts.

Using Raw Expressions

$users = User::selectRaw('count(*) as user_count, status')->groupBy('status')->get();

Raw expressions are powerful but should be used cautiously due to the risk of SQL injection.

Advanced Techniques

Chunking Results

User::chunk(200, function ($users) {
    foreach ($users as $user) {
        // Process the user...
    }
});

This processes large datasets in chunks, reducing memory usage.

Using Cursors

foreach (User::where('active', true)->cursor() as $user) {
    // Process the user
}

Cursors allow you to iterate through database records using a generator, which holds only a single model in memory at a time.

Complex Queries and Joins

$query = User::select('users.*')
    ->join('posts', 'users.id', '=', 'posts.user_id')
    ->where('posts.created_at', '>', now()->subDay())
    ->orderBy('posts.created_at', 'desc');
$usersWithRecentPosts = $query->get();

Custom joins can be more efficient but be sure they are necessary before bypassing Eloquent’s relationship methods.

Database Transactions

DB::transaction(function () {
    // Perform multiple database operations
});

Database transactions ensure that multiple operations behave atomically, which can enhance performance and data integrity.

Performance Testing and Profiling

Testing with libraries like Debugbar or Telescope can provide insights into query performance and potential bottlenecks.

Conclusion

Optimizing Eloquent queries is crucial for maintaining a performant Laravel application. By applying the right techniques at the right time, you can significantly improve the speed and efficiency of your application’s database interactions.

Next Article: Laravel Eloquent: Using whereJSONContains() to filter JSON columns

Previous Article: Laravel Eloquent error: 1615 Prepared statement needs to be re-prepared

Series: Laravel & Eloquent Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array