DEV Community

JohnDivam
JohnDivam

Posted on

Laravel app was slow. Here's how I fixed it

Imagine fetching 100 users, each with related posts and comments.
Without optimization, your app might execute 1000+ queries just to load a single page!
The result? Slow performance, wasted resources, and frustrated users.
Let’s fix it step by step :

1️⃣** Eager Loading ->with()**
Instead of making one query per related record, load relationships in advance.

Before:

$users = User::all();
Enter fullscreen mode Exit fullscreen mode

After:

$users = User::with('posts', 'comments')->get(); //Faster. Cleaner. Efficient.
Enter fullscreen mode Exit fullscreen mode

2️⃣ Database Indexing
Add indexes to columns you frequently query or use in WHERE, JOIN, and ORDER BY clauses.

Example:

CREATE INDEX idx_user_email ON users(email); //Query time improved from 800ms → 50ms
Enter fullscreen mode Exit fullscreen mode

3️⃣ Query Caching (Laravel Cache)
Cache repeated queries or expensive operations using Laravel’s built-in caching.

Example:

$users = Cache::remember('users_with_posts', 60, function() {
    return User::with('posts')->get();
});
Enter fullscreen mode Exit fullscreen mode

4️⃣ Lazy Loading Assets (Images, Videos, etc.)
Optimize your frontend performance by loading assets only when they’re visible.

<img src="image.jpg" loading="lazy" alt="User profile"> 
Enter fullscreen mode Exit fullscreen mode

Faster page rendering and lower initial load times.

5️⃣ Optimize Select Queries (Avoid SELECT *)
Fetching unnecessary columns bloats your response and slows queries.

Before:

$users = User::with('posts')->get();
Enter fullscreen mode Exit fullscreen mode

After:

$users = User::select('id', 'name', 'email')
             ->with('posts:id,user_id,title')
             ->get(); //Only the data you need — smaller payloads, faster delivery.
Enter fullscreen mode Exit fullscreen mode

Final Thoughts
Performance optimization isn’t a one-time task — it’s an ongoing discipline.
By combining eager loading, indexing, caching, and smart query design, you can turn a slow, bloated app into a lightning-fast experience ⚡.

Top comments (1)

Collapse
 
stas_7702602173bf3ccef914 profile image
Stas • Edited

Well, in my opinion, it's pretty basic and mostly revolves around SQL queries.

But the most important thing to start with is not mentioned: Laravel Debug Bar. And then simply open "Queries" tab... Just as easy as that :) It will show you all queries and their timings, all also highlight all duplicated queries.

For even more "advanced" moves:

  • you can start using... explain ;)
  • start using PHP slow log and MySQL slow log
  • you can also install Telescope on top for more visibility\stats
  • you can start using more advanced Laravel caching techniques like flexible caching

And ultimate beaters for "pro" developers:

  • you can install PHP SPX for flame graphs (ultimate level of profiling)
  • you can set up Varnish to get 5-15ms server responses (p.s. carefully, only for some pages that can be fully cached)

Anyway... good luck on your optimisation journey... It's a never-ending loop!