You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As we can see we have a loop in which a lot of queries are produced $this->tags()->get()and $this->load('tags') are called which load all of the tags of the element twice (which can be a real problem if there is a lot of tags) in addition to the creation and count update
This can be upwards of 3 update and 3 select query per addition of one tag (so if you add 5 tags, you have 30 queries)
I think it shouldn't be the job of addTag to load the relation (especially twice on each call), so I would remove $this->load('tags'); and refactor the rest to be more efficient while still solving #26
This could be even further improved by querying all tags at once outside of the loop, or using laravel's sync method and diffing the tags before and after to update the counts
The text was updated successfully, but these errors were encountered:
A quick look into the way tags are added and removed proves to be very inefficient
As we can see we have a loop in which a lot of queries are produced
$this->tags()->get()
and$this->load('tags')
are called which load all of the tags of the element twice (which can be a real problem if there is a lot of tags) in addition to the creation and count updateThis can be upwards of 3 update and 3 select query per addition of one tag (so if you add 5 tags, you have 30 queries)
I think it shouldn't be the job of addTag to load the relation (especially twice on each call), so I would remove
$this->load('tags');
and refactor the rest to be more efficient while still solving #26This would result in only 2 update and 2 select queries in the worst case and 2 insert and 1 select in the best case
Same goes for the
removeTag
method with a best case of just 1 select and a worst case of 1 select and 2 updateThis could be even further improved by querying all tags at once outside of the loop, or using laravel's
sync
method and diffing the tags before and after to update the countsThe text was updated successfully, but these errors were encountered: