DEV Community

Philip Perry
Philip Perry

Posted on • Updated on

3 tips for improving performance of a Laravel website

At the company I work for we tackled performance this last sprint. It had been noticed that the speed in production had become slow. There are of course many reasons for this and front-end also made some changes, but I'm just going to just write some things we did in the back-end.

To find the bottlenecks, we used the excellent tool called Clockwork It uses a composer component to collect the data and to easily view it, you use a browser extension that runs in the dev tools. You could also use Telescope, but Clockwork is more powerful for profiling.

These are 3 of the things we did to improve performance:

1. Eager Loading

Using 'with' for loading related data, one can reduce the number of database queries made. Clockwork is helpful in that it displays how many models are created for a request. This article explains well how eager loading works.

2. Use indexes

Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. Use the EXPLAIN keyword in front of your sql statement to see how many rows are used for the query. Learn more here:

3. Caching

Do you use the same long executing query more than once for a request? In that case save the query result in a registry. I haven't been able to find a website for this, but the basic idea is to use the singleton pattern to make sure you only load the object once and then you check if the data is already in the registry using a key for looking it up.

We also wrote some benchmark tests which will allow us to test in the future if a change has a negative impact on performance.

Top comments (0)