Skip to content
Advertisement

Is there any way to optimize the query while working with 2mil+ rows database table

I am working on Laravel query that shall count the latest month data and also group by week, for the last 3 months. I have tried to fix that in a few ways but still it’s exceeds my memory limit and load very slow. Bellow is the current code that I am using to get the final results – but same problem is also here.

Any idea how to optimize the counting and grouping the data?

JavaScript
JavaScript

Advertisement

Answer

You could use LazyCollections. This should heavily reduce your memory usage.

JavaScript

This also simplifies your logic.

  • $poi->all() will either return an array, empty or not, with the keys.
  • $poi->max() will return the max() of the collection. If the collection is empty, it will return null. A simple ternary operator takes care of that part of your logic as well.
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement