Urgent.News

What's breaking now, across hundreds of outlets.

Tech

I Fixed My Slow Laravel App With 4 Eloquent Changes — Here's What Actually Helped

My Laravel app worked fine for months. Then the users table passed 50,000 rows, the posts page started taking four seconds to load, and a nightly export job started dying with "Allowed memory size exhausted" at 3 a.m. I did not rewrite the app. I did not add Redis. I changed four Eloquent habits, and the same pages got fast again. Here is what actually helped, in the order that mattered most. 1.…

The author's Laravel application had been running smoothly for months until the users table grew to 50,000 rows. This caused the posts page to take four seconds to load and a nightly export job to fail due to memory exhaustion. The author did not rewrite the app or add Redis, but rather made four changes to how they used Eloquent.

These changes significantly improved the app's performance. The first change was eager loading, which replaced multiple queries with just two. The second change was replacing count() with exists() to check for the existence of an email, reducing the workload on the database. The third change was chunking the data to avoid exhausting memory when processing large amounts of records.

The final change was letting the database handle simple calculations instead of fetching rows to compute them in PHP. The author emphasizes the importance of identifying and fixing these common Eloquent issues, using Laravel Telescope to measure the performance improvements. They invite readers to share their own experiences fixing slow Eloquent queries.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

@MockBean Is Gone: Fixing Your Tests After Upgrading to Spring Boot 4

You bump the Spring Boot version to 4, run the build, and the main code compiles fine. Then the tests fail with a wall of errors like these: cannot find symbol symbol: class MockBean package…

  • Update test dependencies: Add separate starters for each technology in Spring Boot 4.
  • Replace @MockBean with @MockitoBean: Move @MockBean to Spring Framework and stub mock objects.
  • Adjust test imports: Move WebMvcTest annotation to new package and change test runner for JUnit 5.

Builds are the new bottleneck

The world is going to need more compute. Agents produce more software, and that software needs to be built and tested. When we started building infrastructure at Tuist , we expected that demand to…

  • Tuist developed Struo to address repetitive dependency compilation.
  • Struo connects existing projects to new caching and remote execution infrastructure.
  • Simple incremental adoption allows teams to optimize compute usage without workflow changes.

More from Thursday 26 November →