DEV Community

Cover image for Why Your Laravel App Is Slow (And How to Fix Database Performance)
Md Fahim Tayebee
Md Fahim Tayebee

Posted on

Why Your Laravel App Is Slow (And How to Fix Database Performance)

Most Laravel apps don’t slow down because of bad code…
They slow down because of inefficient database queries.

As your application scales, your database becomes the real bottleneck.

I’ve seen production apps struggle with:
Hundreds of unnecessary queries
Memory overload from large datasets
Slow dashboards and reports

Full detailed guide (with more examples):

Laravel Database Optimization Guide: Upsert, Performance & Scaling Tips

Learn Laravel database optimization techniques using upsert, bulk inserts, chunking, eager loading, and query performance best practices to build fast, scalable applications.

favicon fahimtayebee.com

1. Stop Looping — Use upsert()
If you’re doing this:

foreach ($products as $product) {
Product::updateOrCreate(['sku' => $product['sku']], $product);
}

You’re running N queries ❌

✅ Use this instead:
Product::upsert($products, ['sku'], ['name', 'price']);

Why it matters:

  • Single query instead of multiple
  • Huge performance boost
  • Perfect for bulk sync & imports

2. Use insert() for Pure Bulk Inserts
User::insert($users);

Benefits:

  • Fastest insert method
  • Minimal overhead
  • Ideal for seeding or imports

3. Use firstOrCreate() Only When Needed
User::firstOrCreate(
['email' => 'user@example.com'],
['name' => 'User']
);

✔ Good for data integrity
❌ Bad for large datasets (multiple queries)

4. Process Large Data with chunk() & cursor()
Loading everything at once = 💥 memory crash

Use chunk:
User::chunk(100, function ($users) {
foreach ($users as $user) {
// process
}
});
Or cursor (better for huge data):
foreach (User::cursor() as $user) {
// process one record at a time
}

5. Fix the N+1 Query Problem
❌ Bad:
$posts = Post::all();
foreach ($posts as $post) {
echo $post->author->name;
}

✅ Good:
$posts = Post::with('author')->get();

👉 This can reduce 100+ queries → just 2 queries

6. Use Raw Queries for Heavy Operations
DB::statement('UPDATE users SET active = 1 WHERE last_login > NOW() - INTERVAL 30 DAY');

When to use:

  • Bulk updates
  • Complex queries
  • Performance-critical operations

7. Quick Optimization Wins
✔ Add indexes to frequently queried columns
✔ Select only needed fields:
User::select('id', 'name')->get();

✔ Use caching:
Cache::remember('users', 60, fn() => User::all());

✔ Move heavy tasks to queues

💡 Final Thoughts

High-performance Laravel apps are not about avoiding Eloquent…

They’re about using the right method at the right time.

Small changes like:

  • Using upsert()
  • Fixing N+1 queries
  • Processing data efficiently

👉 Can turn a slow app into a scalable system.

🚀 Want the Full Breakdown?

I’ve covered everything in detail (with more examples & explanations):
👉 https://fahimtayebee.com/laravel-database-optimization-guide/

👋 Let’s Connect

If you're building:

  • SaaS platforms
  • E-commerce systems
  • High-performance Laravel apps

I share practical insights on scaling and optimization.

Follow me for more 🚀

Top comments (0)