Laravel very slow eloquent query
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Debugging Slow Eloquent Queries in Laravel: Mastering Database Performance
As a senior developer, I often encounter situations where seemingly simple database queries execute far slower than expected. This is a common pain point when dealing with large datasets, especially when working with relational data within the Laravel framework. The scenario you described—fetching catalogs from a table with 14,000 records and longtext columns—is a classic example of where performance bottlenecks hide.
Let's break down why your Eloquent query might be slow and how we can optimize it using best practices.
Understanding the Bottleneck: Why is the Query Slow?
The core issue often isn't the number of rows itself, but how the database has to find those rows. When you fetch data using methods like where() combined with ordering (latest()), the database engine needs to perform operations that are highly dependent on indexing.
In your specific case:
- Full Table Scans: If there is no appropriate index on the columns used in your
WHEREclause (likeshop_id) or for sorting (likecreated_at), the database has to scan every single row to find a match, which is incredibly slow on larger tables. longtextOverhead: Storing large text fields (longtext) consumes more I/O and can slow down data retrieval, especially if you were attempting to fetch these columns unnecessarily. Even though you mentioned avoiding them in the final selection, the underlying table structure impacts overall read performance.- Database Indexing: The speed difference you observed between phpMyAdmin and Laravel often points directly to how effectively the SQL is being executed by the underlying database (MySQL, PostgreSQL, etc.).
Optimization Strategy: Indexing and Selection
To drastically improve the performance of this query, we need to focus on optimizing the database schema and refining the Eloquent query itself. This aligns perfectly with the principles discussed in modern Laravel development, emphasizing efficiency and clean code—a core philosophy championed by teams focused on robust architecture like those at laravelcompany.com.
1. The Power of Database Indexes
The single most effective way to speed up lookups is by creating proper indexes. For a query that filters by shop_id and sorts by created_at, a composite index on these columns will allow the database to jump directly to the required data without scanning the entire table.
Actionable Step: Ensure you have appropriate indexes defined on your foreign keys and frequently queried columns.
If you are using MySQL, you should ensure indexes exist on:
shop_id(for filtering)created_at(for ordering/sorting)
2. Refine Your Eloquent Query Selection
You correctly noted trying to avoid fetching large fields like longtext if they aren't needed for the immediate operation. Always practice selecting only the columns you actually need. This reduces the amount of data the database has to read from disk and transfer over the network.
Your original query:
$catalogs = Catalog::where('shop_id', $shop->id)
->latest()
->get(['id','title', 'created_at', 'shop_id', 'cover_bg', 'frontpage', 'pdf', 'clicks', 'finished']);
This part is already good because you explicitly selected only a few columns. However, if the table is structured poorly (e.g., unnecessary large columns being indexed), optimization becomes harder. Focus on ensuring that any index created supports these specific filter/sort operations efficiently.
3. Advanced Optimization: Using Raw Queries or Eager Loading (Context Dependent)
If simple indexing isn't enough, you can sometimes gain more control by using raw SQL or strategically employing eager loading if this data is part of a larger relationship. For highly complex reporting queries that involve multiple joins and heavy aggregations, optimizing the query structure directly in SQL (while keeping Laravel for model interaction) can be beneficial.
For instance, if you were fetching related items, Eloquent's eager loading mechanism is invaluable:
// Example of efficient retrieval using relationships (if applicable)
$shop = Shop::find($shopId);
$catalogs = $shop->catalogs()->latest()->get(); // Assuming a relationship exists
Conclusion
Slow database queries are rarely an indication that the server is fundamentally broken; they are usually a signal that the data access layer needs refinement. By focusing on proper indexing, minimizing data retrieval, and understanding how your database engine processes requests, you can transform sluggish operations into lightning-fast interactions. Always treat your database schema as the foundation of your application's performance, ensuring every query is built for speed from the ground up.