-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.7] Improve eager loading performance on MySQL #26434
Conversation
Given this is a performance related PR - would be worth providing some performance comparisons to see if the changes actually make a meaningful difference? |
You have a performance comparison posted in the issue ticket -> #26051 (comment) |
Just as a quick headline statistic from the referenced issue (#26051), some examples show a performance improvement of over 90% in query time |
I don't see anything mysql specific, is this change for all drivers then? |
@mfn Yes, the change affects all drivers. We could make it MySQL-specific, but I don't think that makes sense. All databases benefit from a raw query in this case, even if the performance improvement is smaller than on MySQL. |
Awesome, and well, great to see it's already merged; thx! |
Do we really need the
|
I'd think of replacing
|
@vlakoff The purpose of |
Ok, let's just forget about my Still, I'd suggest renaming I'd also suggest replacing the foreach ($values as &$value) {
$value = (int) $value;
} |
There is an old bug in the
PDO
implementation for MySQL that slows down largesWHERE IN
queries. When the query has a few thousand bindings, the time execution time offetchAll()
increases non-linearly.In Laravel, this especially affects eager loading. For models with integer keys, we can fix this by using raw
WHERE IN
clauses. The newwhereInRaw()
method appliesintval()
to all values, so this is as secure as a prepared statement.It's certainly not a very elegant solution but still worth considering for the core. Since most Laravel sites run on MySQL/MariaDB and most sites use eager loading with integer keys (my assumptions), this would improve the performance on a lot of sites – even if they aren't eager loading thousands of records at once. The individual query may only gain a few milliseconds, but over all sites combined, this adds up.
This PR shows a sample implementation for
HasMany
relationships. The same changes would apply to all the other relationships.Fixes #26051.