Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Simoo105's avatar

What are the best practices for optimizing the performance of a Laravel dashboard that needs to query multiple tables?

Hello everyone, I'm trying to build the backend(Laravel) for a huge dashboard that have multiple analytics, for every stat I'm using different table, I created my queries at least 7 of them for now, but I cant afford calling multiple queries in same page. The dashboard is very big and complex that's why I cant join all tables together, but I'm concerned about the performance impact of running all these queries on one page. How can I optimize my queries or structure my dashboard to avoid performance issues? What is the best procedure to deal with this type of dashboards? maybe there's some patterns or something I can follow, I don't know. please enlighten me.

I expect maybe some logic in Laravel or some structure I can follow to deal with this issue. how other developers deal with this. Maybe some full tutorial step by step. Thanks in advance šŸ™

0 likes
7 replies
LaryAI's avatar
Level 58

The best way to optimize the performance of a Laravel dashboard that needs to query multiple tables is to use eager loading. Eager loading allows you to load related models in a single query, reducing the number of queries that need to be executed. To use eager loading, you can use the with() method on your query. For example:

$dashboard = Dashboard::with('stats')->get();

This will return all the stats associated with the dashboard in a single query. You can also use the load() method to eager load nested relationships. For example:

$dashboard = Dashboard::with('stats.metrics')->get();

This will return all the stats and metrics associated with the dashboard in a single query.

You can also use the chunk() method to break up large datasets into smaller chunks. This can help reduce the amount of memory used when loading large datasets. For example:

Dashboard::chunk(100, function ($dashboards) {
    foreach ($dashboards as $dashboard) {
        // Do something with the dashboard
    }
});

Finally, you can use caching to reduce the number of queries that need to be executed. For example, you can use the remember() method to cache the results of a query for a certain amount of time.

$dashboard = Dashboard::remember(60)->get();

This will cache the results of the query for 60 minutes.

By using eager loading, chunking, and caching, you can significantly reduce the number of queries that need to be executed and improve the performance of your Laravel dashboard.

Tray2's avatar

It depends , but if you can't make a single query, and you don't want to make several, well then it's not doable. You should not be afraid to make several queries, as long as they all are performant you should be good.

However, you do have the option of using javascript to fetch each part of the dashboard , with a nice spinner for each part.

Another option is to store the data you want to display in tables with the calculated value, so you don't have to calculate on each fetch.

Without any code examples it's very hard to give solid advice, but as the AI-bot says, don't load everything at once.

1 like
Simoo105's avatar

@Tray2 Thanks for your reply. for now I have 5 queries and there's more than (others are in progress). I just need maybe a structure or pattern to follow to make this queries dynamic and reusable. some queries are grouped by weeks. some contains overall data. If you have a big complex dashboard what you will do in my place? whats the pattern you will follow? maybe a tutorial that that deals will complex dashboard can help. Thanks

jlrdw's avatar

Who is the analytics for, just you?

1 like
Simoo105's avatar

@jlrdw No its a dashboard for users (complex project where i need to calculate all sort of analytics and display it for them).

jlrdw's avatar

@Simoo105 also are you using cache? How often are you doing a requery? You could also have a button that lets each user choose when to update their dashboard, usually you don't need updates every few seconds some may want to go longer before seeing an update..

Just a suggestion.

But as @tray2 said a few efficient queries should be fine.

1 like
Simoo105's avatar

@jlrdw yes I'm using cache and my queries are fine I mean fast. If calling multiple queries won't be a problem and my queries are done(10 or more queries), how I'm suppose to call them? if you can give me a tutorial or a pattern to follow.. thanks

Please or to participate in this conversation.