Laravel Performance | Be Careful with withCount()
An example of query optimization using withCount vs join, also adding cache on top.
2025-09-14 07:00:00 - Laravel Daily
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