Eloquent, making many small indexed queries, is much better than doing one large query for a wide variety of reasons:
- MySQL does not have to load multiple tables into temporary memory for each query and can reuse them between queries
- SQL optimizer will work faster with every query
- It allows you to cache your results without throwing
JOIN (and other similar offers) from your data, which simplifies caching
You don't really notice this, but the path used by the SQL optimizer is the same between the following queries:
SELECT a.*, b.* FROM a INNER JOIN b ON (a.id=b.id) WHERE a.id = 1 SELECT a.*, b.* FROM a, b WHERE a.id = b.id AND a.id = 1
Both of them will force the SQL optimizer to execute these queries under the hood:
SELECT a.* WHERE a.id = 1 SELECT b.* WHERE b.id = 1
And from there, depending on your indexes, the SQL optimizer will perform a match based on either individuals or the full table data. What does Eloquent do? These are the two questions. You donโt get anything for one big request - in fact, you lose the ability to reuse data. In all, prefer small, optimized, reusable, cached queries to bulky operators.
source share