Laravel Order By Mutator: What About Performance?

Laravel Order By Mutator: What About Performance?

Laravel Daily

2 года назад

8,772 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@planchet2013
@planchet2013 - 12.06.2023 22:02

thank you a lot!

Ответить
@AliAwwad
@AliAwwad - 25.03.2022 16:14

Just few days ago I used raw ordering, I needed a way to pin 📌 the currently selected item to the top of a returned collection so it shows in a grid always on top in the edit blade view .

Ответить
@d.a.9933
@d.a.9933 - 23.03.2022 15:49

That’s not a mutator it’s an accessor.

Accessor: getFullNameAttribute

Mutator: setFullNameAttribute

Ответить
@bhushanchandel3567
@bhushanchandel3567 - 23.03.2022 14:47

How should we do data sorting for encrypted data in db table, say for example firstname and lastname of users table ?

Ответить
@jobsphil9553
@jobsphil9553 - 21.03.2022 12:37

mutator must not be used for query condition.

Ответить
@inso4606
@inso4606 - 21.03.2022 11:00

In almost every situation is better to do that kind of actions on db level.

Ответить
@mityukov
@mityukov - 20.03.2022 19:29

What is Accessor, then?..

Ответить
@gabrielsales9350
@gabrielsales9350 - 20.03.2022 05:20

Thank you, what is theme you use in phpstorm ?

Ответить
@intipontt7490
@intipontt7490 - 19.03.2022 03:12

Mutators can also get pretty ugly. I've seen some that depend on loaded relationships. Trying to sort by them will absolutely kill performance.

For this particular query, the best improvement I can think about is to take advantage of lazy collections at the end. ( cursor() instead of get() ). Who knows? It might even make the performance drop from the full_name mutator be negligible.

Ответить
@RshuDylan
@RshuDylan - 19.03.2022 02:05

Thank you! Can I use sortby on encrypted field?

Ответить
@donmikele07
@donmikele07 - 19.03.2022 01:02

Great example, explanation and analyse!

Ответить
@firdavs.ibodullayev
@firdavs.ibodullayev - 18.03.2022 19:54

I did
->orderBy('first_name')
->orderBy('last_name')
->get()

Ответить
@Flankymanga
@Flankymanga - 18.03.2022 18:36

Even better would be just to create a permanent column for full_name in the database table with before insert trigger where i would concatenate first and last name into this new value. Depending on the size of the dataset concatenation could be very expensive to do on millions of rows in table on the fly for the purpose of ordering.

Ответить
@MrStealthWarrior
@MrStealthWarrior - 18.03.2022 18:16

To keep that code clean and bug free probably there should be a way to describe a way to use mutators in orderBy, where and may be some other functions. Like we have get and set for an attribute. After some time and migrating fully to new attribute syntax it would be a good thing to implement by someone who actually needs this in their projects.
So you can keep your code related to the mutator in the single place.

Ответить
@elmarzougui
@elmarzougui - 18.03.2022 16:55

Amazing things thank you povilas

Ответить
@rahmanramsi
@rahmanramsi - 18.03.2022 14:43

Why not use Virtual Column on database level?

Ответить
@tayyabshahzad9933
@tayyabshahzad9933 - 18.03.2022 14:39

Thanks :)

Ответить
@saravanasai2391
@saravanasai2391 - 18.03.2022 14:00

Hi sir ..... As as junior dev . i am not having exposure to Android development but in my company there area lots of project comes on mobile development is Flutter is good to go & does it helps to build my career or else moving to Native android development is good . give a suggestion sir thanks in advance sir

Ответить
@omidmollaalizadeh6443
@omidmollaalizadeh6443 - 18.03.2022 12:40

Can we also combine 'where' with 'CONCAT' in DB level? where full_name like something for searching purposes.

Ответить
@amitdev1485
@amitdev1485 - 18.03.2022 11:35

loved it...

Ответить
@cykablyatii2086
@cykablyatii2086 - 18.03.2022 10:45

why not just use the last name if you're gonna sort out the records by Lastname + Firstname ?
in that way, you'd avoid concatenating the strings in the query

Ответить
@ward7576
@ward7576 - 18.03.2022 10:19

So, in the spirit of debate, I ran both concat and 2 order by method on sorting 2 columns together.

With concat: 0.00047 sec (0.47 ms) on average,
with 2 order by clauses: 0.111 sec (111 ms) on average.

I ran it against ~30,000 records (a bit lower than that).

Therefore it's significantly quicker to just concat the columns on MySQL side.

Ответить
@ehsankhan6857
@ehsankhan6857 - 18.03.2022 10:18

sir you are great

Ответить
@DragosBurciu
@DragosBurciu - 18.03.2022 09:13

Could use 2 order by instead of the concat. I don't think it helps unless you want to use that concat in the view directly, but in our case we have the accesor. I would use 2 order by calls , one on last name and second on first name and then use the accesor for the full name on the models. :) wonder if that would be faster as it would not concat 10k rows :)

Ответить
@GergelyCsermely
@GergelyCsermely - 18.03.2022 09:07

Thanks, Not needed to conctat the 2 fields for ordering in my opinion, 2 orderby after eachoter

Ответить