Laravel Polymorphic Many-to-Many Performance: Query Pivot Table

Laravel Polymorphic Many-to-Many Performance: Query Pivot Table

Laravel Daily

1 год назад

12,223 Просмотров

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


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

Thomas A. Anderson
Thomas A. Anderson - 31.08.2023 17:52

nice tip, thanks.

Ответить
Wade Zimmerman
Wade Zimmerman - 13.08.2023 17:39

Wherehas is usually an indicator to me that a query needs to be optimized.

Ответить
Rico Hertha
Rico Hertha - 07.08.2023 03:34

I guess, in your solution there are to much repeatiations in the column "assignable_type". It's a more cleaner design If there is a separate assignment-table for each type. to merge these different assignment-tables back into one normalized presentation (if needed) you can simply create a db-view "assignables". In this view can declared how the different assignment-tables (virtual) merged back into one normalized presentation (by union). Also it's a good idea to create multiple views for multiple use-cases - prevent godviews. like in oop you can combine "abstract" views to more specialized ones. benefits are: use much less storagespace, much better index usage and maintaining (remember: "or" is an index-killer), less table-locks at read/write/delete operations, allows using of foreign-keys to protect data-consitence and use traversal update/delete ... views hides complexity

all together results in:
much better read/write performance especialy if count of assignments grows up over the time (over many years),
query-optimizer can cache queryplans,
separation of concerns and decoupling (no tech debt -> all types must not share the same assignment-structure, but can normalized to a common structure by view).
better overview and documentation for old and new team members

I know, maybe its sounds greatly exaggerated for this simple example. But it's so important, to invest in a good architectur at the beginning of a softwareproject.
Try to avoid "i refactor this part later" because often "later equals never"

"quick and dirty" must have a very good reason - it can be the beginning of software-degeneration and can be so expensive in the future (tech dept). "there was no time" is a lame excuse!!! building good architectures needs time / same like write good books needs time. at the end you have the responsibility for your work - and maybe you must maintain it for a long time.

You should be proud about what your have created - you shouldn't have to be ashamed of it

And please use a uid for every datarow (auto-id, uuid ...)! Also every book has page-numbers ;)

Ответить
resort16
resort16 - 05.08.2023 23:15

Just use joins, not whereHas. And performance will be even better. Try it.

Ответить
GringoDotDev
GringoDotDev - 02.08.2023 23:48

Truthfully this is why I generally prefer to use Eloquent more as a query builder than an ORM.

Ответить
Gergely Csermely
Gergely Csermely - 02.08.2023 19:09

Thanks

Ответить
juan pineda
juan pineda - 02.08.2023 17:34

Great Stuff, and performance gains are huge!

Ответить
mzerone
mzerone - 01.08.2023 20:08

I think have solution will be better and the solution just make index on the feild you want search by

Ответить
Ofi Videos
Ofi Videos - 01.08.2023 15:30

Awesome!

Ответить
Mick Davies
Mick Davies - 01.08.2023 15:24

Great tip! I’ve learned something new 🎉

Ответить
mahmoud adel
mahmoud adel - 01.08.2023 14:46

Great, but for an better performance don't use assignable_type as a string make it integer and with adding an combined index with task_id and assignable_id and assignable_type u will get a more better performance query

Ответить
Adil Mughal
Adil Mughal - 01.08.2023 14:21

Nice ❤

Ответить
ADIL IZM
ADIL IZM - 01.08.2023 14:09

wonderful and easy to understand thanks a lot sir

Ответить
Killionaire
Killionaire - 01.08.2023 11:17

There is no such thing as a many-to-many relationship from the eyes of a true software architect. There is only one-to-many relationships - even if that means creating pivot models.

Ответить
Mohamad Cheaib
Mohamad Cheaib - 01.08.2023 09:59

Great 👍 , Isn't even faster if we move the repetition of the condition where assignable_id =... outside to be and whereIn('assignable_id',[auth()->id, auth()->user()->group_id, auth->user()->position_id])?

Ответить
Bülent
Bülent - 01.08.2023 09:27

Nice.

Ответить
Francesco Leto
Francesco Leto - 01.08.2023 08:34

Good advice 😊

Ответить
Sixtus Miracle Agbo
Sixtus Miracle Agbo - 01.08.2023 08:04

Amazing, the problem is with the sub queries with orWhereHas relation. It's a problem common across ORMs. Query performance and getting the best methods to use in order to avoid performance issues.

Ответить
Zikri Endisyah Munandar
Zikri Endisyah Munandar - 01.08.2023 08:00

cool vids

Ответить