TechnicalEloquent performance

Eloquent performanceLesson 1 of 28

Measuring Your Database Performance

Measure before you optimise. When in doubt, check the Models count.

2 min readIntermediateLesson

Eloquent performance01

The problem

You can't fix slowness you can't see. Problems that feel fine on your laptop get much worse with real production data.

The fix

Install Laravel Debugbar locally and keep an eye on four numbers on every page.

Results

Sort by name, query time

~30 ms<5 ms

83% lower

after adding an index

Models loaded with $with = ['users']

32832

memory only rose ~2 MB

How to do it

composer require barryvdh/laravel-debugbar --dev

The four numbers to watch:

  • Request time. Aim for under 200 ms, ideally under 100 ms.
  • Query count. A sudden jump (say 2 to 16) usually means an N+1 problem.
  • Query time. One slow query is enough to drag a page down. Often an index fixes it.
  • Models count. How many Eloquent objects were built. This is the clearest warning sign, clearer than memory.

Watch out

  • Install Debugbar as a dev dependency only. In production it exposes sensitive data.
  • Be wary of protected $with on models. It loads relations everywhere, even where you don't need them.

Frame from lesson 1

Debugbar's Models tab after adding $with = ['users'] to Company: 16 companies but 816 users hydrated (832 models), while memory only reads 6 MB. This is why the Models count is a clearer warning sign than memory.

Key takeaway

Measure before you optimise. When in doubt, check the Models count.

Check yourself

Memory went from 4 MB to 6 MB after a change. Safe to ignore?

Reveal answer

✓ Answer

Not necessarily. Check the Models tab. In the lesson, that small memory bump hid a jump from 32 to 832 models.