You can't fix slowness you can't see. Problems that feel fine on your laptop get much worse with real production data.
Sort by name, query time
~30 ms<5 ms
83% lower
after adding an index
Eloquent performanceLesson 1 of 28
Measure before you optimise. When in doubt, check the Models count.
2 min readIntermediateLesson
You can't fix slowness you can't see. Problems that feel fine on your laptop get much worse with real production data.
Install Laravel Debugbar locally and keep an eye on four numbers on every page.
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
composer require barryvdh/laravel-debugbar --dev
The four numbers to watch:
protected $with on models. It loads relations everywhere, even where you don't need them.
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.
Measure before you optimise. When in doubt, check the Models count.
Memory went from 4 MB to 6 MB after a change. Safe to ignore?
✓ Answer
Not necessarily. Check the Models tab. In the lesson, that small memory bump hid a jump from 32 to 832 models.