laravel-daily 6 hours ago
Povilas Korop #Web Development

Laravel Performance | Be Careful with withCount()

An example of query optimization using withCount vs join, also adding cache on top.

I have just done a similar optimize last week, removed a withCount with a join. I don’t know what MySQL is doing with subquery, but this looks like a n+1 problem.


To me it looks like MySQL is doing subquery on each main row, so time needed explodes, and internal cache in MySQL properly becomes tainted on larger dataset, so it explodes even more.


Just my theory, but the change is extreme

Laravel Daily
Povilas Korop