Slide 1

Slide 1 text

Using Eloquent for performance design patterns Nemanja Marić maki10

Slide 2

Slide 2 text

About me ● PHP Developer at Abstract ● Member of PHP Serbia Community ● Co-organizer Laravel Serbia Meetup’s ● Working with PHP since 2014 ● In Laravel world from 2016 ● Open source contributor ● Contributing to the Laravel Framework ● And most !important: Husband and father of two little angels

Slide 3

Slide 3 text

Interesting part Share your knowledge with others to get some knowledge.

Slide 4

Slide 4 text

Over the years

Slide 5

Slide 5 text

Everything we need is: Laravel debugbar, reinink/advanced-eloquent < Laravel 6

Slide 6

Slide 6 text

Let’s start

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Run super fast

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Proudly generate with beyondcode/laravel -er-diagram-generat or

Slide 11

Slide 11 text

Requirement number 1 For logged user show only results where club is matched and his buddies.

Slide 12

Slide 12 text

Add UserPolicy - php artisan make:policy UserPolicy

Slide 13

Slide 13 text

It’s work. Let’s see debugbar

Slide 14

Slide 14 text

But why? Queries are super fast, but why is memory and execution time so big and what is this last 1012 models? Does someone know?

Slide 15

Slide 15 text

Answer Because we do to much jobs in PHP and not in database. 1012 Models are custom metric in debugbar added by @reinink (Jonathan Reinink), is available from v3.2.6, it’s called “Hydrated Models” “It provides a quick count of how many Eloquent models were created during a request...which can have a huge impact on performance.“

Slide 16

Slide 16 text

Let’s fix issue

Slide 17

Slide 17 text

Rerun, and we are good to go further

Slide 18

Slide 18 text

Requirement number 2 Order table data in two way: 1. Order by buddies first 2. Order by name

Slide 19

Slide 19 text

This is heavy because we need to order by Pivot table. Everyone who sorted results like this knows that is this very heavy, unless? Order by buddies first, why is heavy?

Slide 20

Slide 20 text

Order by custom query function `orderBySub`

Slide 21

Slide 21 text

Query builder custom macro

Slide 22

Slide 22 text

Order First By Buddies then name

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Perfect

Slide 25

Slide 25 text

Requirement number 3 Add last trip date.

Slide 26

Slide 26 text

Add last trip date, hmm?

Slide 27

Slide 27 text

Peace of cake

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Wow, hmm, ok ok I forgot eager load

Slide 30

Slide 30 text

Oh 250 000 trips results show faces and punch back

Slide 31

Slide 31 text

Let’s force N+1. N+1 is not always worst choice

Slide 32

Slide 32 text

N+1 cost less resources. So is it better? Can be better?

Slide 33

Slide 33 text

Yes via custom builder function `addSubSelect`

Slide 34

Slide 34 text

We need to tweak a bit

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

We are back in business

Slide 37

Slide 37 text

Requirement number 4 Add last trip lake

Slide 38

Slide 38 text

Add withLastTripLake scope

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No impact on performance at all

Slide 41

Slide 41 text

Requirement number 5 Let’s make last trip clickable so we can go to actual trip.

Slide 42

Slide 42 text

Stop for second

Slide 43

Slide 43 text

Let’s add dynamic property

Slide 44

Slide 44 text

Remember?

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Replace old scopes

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Super easy, super fast

Slide 49

Slide 49 text

Questions? Leave review: https://joind.in/user/maki10 Slides: https://speakerdeck.com/maki10 Twitter: https://twitter.com/NemanjaMaki10 All credits go to @reinink and his Laracon 2019 talk. Watch him on Laracon2019 was very good choice, and it’s my advice for you to watch when he kicking a stage.