-1 easy - create a daily job that store your result in a table and use this table for your dashboard
-2 real time - imagine a function that calculate your average from your previous one calculation
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have this query:
$avglisteningtime = TrackTimes::query()->where('user_id', '=', $user->id)->avg('time_listened');
And this is getting the average listening time for all tracks owned by a user, of which there may be thousands of lines. The TrackTimes model looks like this:
ID: 7
CREATED: 2018-11-24 23:10:22
UPDATED: 2018-11-24 23:10:22
TRACK ID: 4
TIME LISTENED: 267 (in seconds)
TRACK TOKEN: p3AtlB92pKvvAwHSrcKdejhOFIzzt2TbSPFOTzDZa6FKwJv4Ho
USER ID: 75
For any one track there may be thousands of the above, and for any one user there may be dozens of tracks.
The app is getting quite big now with a lot of users and plays so this query is starting to slow down the dashboard page load.
I would like to either optimise the query OR is there a way to make the page load and have this data fill in after the fact, once it has been generated?
Thanks
Please or to participate in this conversation.