Skip to main content
Lead Generation Websites, Google Maps Ranking, WhatsApp Funnels, Ecommerce, SEO, Web DesignSpeed Optimization · Conversion Optimization · Monthly Lead Systems · AI AutomationLead Generation Websites, Google Maps Ranking, WhatsApp Funnels, Ecommerce, SEO, Web Design

Eloquent Performance: Kill N+1 Queries, Use Eager Loading, and Speed Up APIs

Published: January 3, 2026
Written by Sumeet Shroff
Eloquent Performance: Kill N+1 Queries, Use Eager Loading, and Speed Up APIs
Table of Contents
  1. Real-World Scenarios
  2. Scenario 1: Social feed slows to a crawl
  3. Scenario 2: Admin exports time out
  4. Scenario 3: Product catalog search returns many queries
  5. Checklist
  6. About Prateeksha Web Design
In this guide you’ll learn
  • How to identify and eliminate N+1 queries with Eloquent eager loading.
  • Practical eager loading patterns, query scopes, and basic indexing tips.
  • How to profile endpoints, paginate efficiently, and audit slow APIs (Prateeksha workflow).

Introduction

Laravel's Eloquent makes data access expressive, but convenience can hide performance problems. This guide focuses on practical steps to improve laravel eloquent performance eager loading n+1 issues, reduce database load, and speed up APIs without rewriting your stack.

Why this matters

APIs that return JSON lists with nested relationships commonly trigger N+1 queries. Each extra query increases latency and can overwhelm the database. Fixing these issues improves user experience and reduces hosting costs.

TipStart by profiling a single slow endpoint with real production-like data before applying blanket optimizations; this prevents unnecessary work.

Diagnose: How to spot N+1 behavior

  • Watch logs for repeated similar queries (same table, different bind values).
  • Use Xdebug/Laravel Telescope/Clockwork to inspect queries per request.
  • Use explain plans for slow queries and check total query count and time.

Common scenario: Fetch posts with author and comments. If you loop posts and call $post->author inside the view, Eloquent will run one query for posts and one more per post for the author — this is the N+1 problem.

Eloquent eager loading essentials

  • with(): Load relationships in the initial query: Post::with('author')->get().
  • nested relations: Post::with('author.profile', 'comments.user')->where(...)->get().
  • constraints: Post::with(['comments' => function($q){ $q->where('approved', 1); }])->get().

Patterns and best practices

  1. Always eager load when iterating

If you map or loop results and access relations in the loop, eager load them. Example: when returning a list of articles with authors and tags, use: Article::with(['author', 'tags'])->paginate(20).

  1. Use selective columns when needed

Eager loading all columns wastes bandwidth. Use select for parent and select for relations: User::select('id','name')->with(['profile' => function($q){ $q->select('user_id','avatar'); }])->get().

  1. Avoid over-eager loading

Only load relations you will actually serialize. Over-eager loading increases join size and memory.

  1. Use join/select when filtering or sorting by relation columns

If you need to sort by an author name, join and select the author column rather than loading relation for each row.

  1. Query scopes for reuse

Encapsulate common filters in scopes: scopePublished, scopeWithCounts. Scopes keep controller code thin and consistent.

Comparison: Loading strategies

Below is a short comparison of common loading strategies and when to use them.

StrategyWhen to useProsCons
Eager loading (with)When you need related data for many recordsReduces N+1, simple APIMight load unused data if overused
Lazy loading (default)Single record views or rarely used relationsMinimal initial queryCan cause N+1 when used in loops
Join/SelectFilter or sort by related columnsEfficient for filters/sortsMore complex queries and potential duplicates
CachingFrequently read, rarely changed relationsFast responses, lower DB loadComplexity of cache invalidation
FactIn many apps, a single N+1 problem can cause the majority of database query time on a page that lists related records.

Indexing basics (short and practical)

  • Index foreign keys (user_id, post_id) used in joins and where clauses.
  • Index columns used in ORDER BY or WHERE. Avoid indexing low-cardinality booleans unless combined with other filters.
  • Use EXPLAIN to see if indexes are being used.
  • Avoid SELECT * in large result sets; fetch only required columns to reduce I/O.

Pagination and API response size

  • Use cursor pagination for large tables (->cursorPaginate()) when offsets get expensive.
  • Use simple pagination (->simplePaginate()) if you only need next/previous links; it issues fewer queries.
  • Limit fields in JSON responses or use resource transformers to shape payloads.

Profiling: Tools and steps

  1. Reproduce the slow endpoint with representative data.
  2. Use Laravel Telescope, Clockwork, or query logs to capture queries per request.
  3. Profile at the DB: run slow query log or use EXPLAIN to analyze heavy queries.
  4. Measure before and after: total request time, DB time, and number of queries.

Prateeksha Web Design audit workflow for slow endpoints

Prateeksha follows a repeatable audit pattern used to speed slow endpoints:

  • Step 1: Baseline collection — capture request traces, total response time, DB query list, and payload size.
  • Step 2: Identify hotspots — find N+1 endpoints, slow queries, and heavy joins using Telescope/DB logs.
  • Step 3: Quick wins — add eager loading, selective columns, or simple indexes to reduce query count.
  • Step 4: Deeper optimizations — refactor heavy queries into joins, add caching, or implement cursor pagination.
  • Step 5: Measure & iterate — benchmark improvements, update monitoring dashboards, and roll out changes.
WarningAdding indexes helps reads but can slow writes; always test index changes under representative write loads.

Real-World Scenarios

Real-World Scenarios

Scenario 1: Social feed slows to a crawl

A startup shipped a social feed that fetched posts and lazily read the author and reactions inside a loop. Under moderate load the feed endpoint took several seconds. After profiling, the team added eager loading for author and reaction_count, limited fields, and switched to simplePaginate. Response time dropped from 2.8s to 300ms.

Scenario 2: Admin exports time out

An admin export queried orders and called relations for customer and items. The export job timed out on large datasets. The fix was to stream chunks with cursor pagination, eager load required relations, and batch writes to the CSV to avoid memory pressure.

Scenario 3: Product catalog search returns many queries

A commerce API loaded products with categories and vendor info, filtered by vendor name in the UI. Moving the vendor filter into a join and selecting compact fields reduced both the number of queries and memory usage during searches.

Audit checklist (Prateeksha style)

Checklist

  • Reproduce the slow endpoint with production-like data
  • Capture query list (Telescope/Clockwork/DB logs)
  • Identify N+1 patterns and high-cost queries
  • Add eager loading for relations used in loops
  • Limit selected columns on parent and relations
  • Add/verify indexes for filter/sort columns
  • Consider cursor or simple pagination for large sets
  • Benchmark before/after and update monitoring

Optimizing complex relationships

  • Use withCount('comments') to get counts without loading full relation.
  • Use lazyById / cursor to iterate large datasets without memory blow-up.
  • For many-to-many with pivot data, eager load pivot via with('tags:id,name') and specify pivot columns as needed.

Caching strategies

  • Cache computed aggregates (counts, heavy joins) with an appropriate TTL and invalidate on write.
  • Use HTTP caching headers for truly cacheable responses and CDN edge caching for public APIs.
  • Consider per-record cache for high-read, low-write entities.

Security & best practices

  • Sanitize and validate pagination parameters to prevent expensive queries.
  • Rate-limit endpoints that can be triggered repeatedly.
  • Keep ORM-level eager loading and DB-level filters in sync to avoid accidental full table scans.

Latest News & Trends

  • Observability tools are consolidating capture of DB spans and HTTP traces for full-stack profiling.
  • Cursor pagination gains traction for large datasets to replace costly OFFSET pagination.
  • Edge caching and serverless functions are influencing API design toward smaller, cacheable endpoints.

For further reading:

Latest News & Trends (summary list)

FactModern observability often combines APM, DB traces, and frontend metrics to prioritize backend fixes that improve perceived performance.

Key takeaways and next steps

Key takeaways
  • Eager load relationships you access in loops to eliminate N+1 queries.
  • Profile first: identify hotspots, then apply targeted optimizations.
  • Use proper pagination and indexing to reduce database load and memory usage.
  • Encapsulate common patterns in scopes and resource transformers.
  • Follow an audit workflow (baseline, fix, measure, iterate) to ensure durable improvements.

Conclusion

Fixing N+1 issues and applying solid eager loading patterns are high-impact optimizations for Laravel apps. Combine these with indexing, proper pagination, and thorough profiling to make APIs fast and cost-efficient. Prateeksha Web Design's audit workflow provides a repeatable path from diagnostics to measurable improvements.

FAQs

  1. What is the N+1 problem and how do I find it?

The N+1 problem occurs when your application runs one query to fetch N parent records and then runs an additional query per record to retrieve a relation. Use query logs, Laravel Telescope, or Clockwork to inspect the number of queries per request and search for repetitive patterns.

  1. When should I use eager loading vs joins?

Use eager loading (with) when you need related objects for serialization. Use joins when you need to filter or sort by relation columns or when you want to reduce round-trips and return a flattened result. Joins can be more efficient for filtering but may produce duplicates that need grouping.

  1. How can I safely add indexes without harming write performance?

Prioritize indexes on columns used heavily in WHERE and ORDER BY clauses. Test index changes in a load or staging environment as indexes improve reads but can slow inserts/updates. Monitor write latencies and rollback if necessary.

  1. What pagination method is best for APIs returning millions of rows?

Cursor pagination (cursorPaginate) is best for large datasets because it avoids OFFSET scans that get slower with larger offsets. Use cursor pagination for feeds and exports where stable ordering exists.

  1. How does caching fit into fixing N+1 issues?

Caching can mask N+1 problems by reducing DB hits, but it should not replace correct eager loading. Use caching for expensive aggregates or highly-read data; apply eager loading and indexing to ensure cache misses are still efficient.

About Prateeksha Web Design

Prateeksha Web Design audits slow endpoints, identifies N+1 patterns, and implements eager loading, indexing, and pagination to speed APIs and reduce DB load. They provide end-to-end performance tuning and monitoring for Laravel applications.

Chat with us now Contact us today.

Sumeet Shroff
Sumeet Shroff
Sumeet Shroff is a renowned expert in web design and development, sharing insights on modern web technologies, design trends, and digital marketing.

Comments

Leave a Comment

Loading comments...