-
Notifications
You must be signed in to change notification settings - Fork 85
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
Faster version of transWhere #623
Conversation
This PR is an alternative version of rainlab#586 Instead of creating a separate method that has a different behavior to the original implementation, I managed to fix ``transWhere`` itself by utilizing 2 queries instead of the slow orWhere clause. This implementation also preserves the fallback functionality. I made some tests on my project (originally discussed here: rainlab#581) - now I have 2 queries that execute in ~ 5 ms instead of a single 60 second query. The improvement is significant. Comments are welcome.
I really like this implementation @acasar ! |
From initial tests I have been doing, it behaves the way I would expect it to. Can you guys @mariavilaro @mterman @Eoler @aurelien-roy test this to see if you get the same performance improvements as @acasar and report back with numbers? Thanks. |
I did some tests, created 50,000 records for one of my models, and added a translation for those 50,000 records;
So I don't have the kind of improvement I heard of, but it's still a significant (62X) improvement. Good work! |
@acasar Hey, not sure why but this version of transWhere broke searching for slug for random slugs when i rolled it back to older version with leftJoin it works as expected. After little bit of testing looks like its when multiple indexes even different models are with same value not sure why this happens. |
This version of
I don't have slug = 'hello' in DE locale. Slug in DE locale is empty. But I have this slug in the default EN locale. And this query returns me Page where slug = 'hello' in default EN locale instead of As a solution you can add one more parameter to |
It might be worthwhile to keep the old implementation alongside the new one. What to call it? |
I think what @iboved is referring to is to have an option to add a whereTrans clause without a default fallback. Bringing back the original implementation would not solve that because it worked the same way - if translated slug was not found, it checked the default slug with an orWhere clause. Another parameter would need to be introduced to enable that behavior. In few cases where I needed that I created my own scope and queried translated table manually. But it may be nice to have this option by default. This is the scope I use: public function scopeTransWhereNoFallback($query, $index, $value, $locale = null, $operator = '=')
{
$translateIndexes = \Db::table('rainlab_translate_indexes')
->where('rainlab_translate_indexes.model_type', '=', $this->getMorphClass())
->where('rainlab_translate_indexes.locale', '=', $locale ?: app()->getLocale())
->where('rainlab_translate_indexes.item', $index)
->where('rainlab_translate_indexes.value', $operator, $value)
->pluck('model_id');
return $query->whereIn($this->getQualifiedKeyName(), $translateIndexes);
} |
Got it. Something added for this here: fdec343 |
Thanks, guys, for the quick response and solution! |
Actually you should just use a normal where for the default locale, since you don't want to query translatable data in this case. $trans = Translator::getInstance();
if($trans->getLocale() === $trans->getDefaultLocale()) {
$post = Post::where('slug', $slug)->first();
} else {
$post = Post::transWhereNoFallback('slug', $slug)->first();
} But I agree that this condition should probably be handled within Currently we have the following situation. Imagine we have 2 Posts:
If we use the following where conditions in en locale, we get these results:
* No result without this PR: #685 If we use the following where conditions in sl locale, we get these results:
But I think what we actually want when we run the query in sl locale is this:
So currently no implementation of Potential problem could arise if one of the translated slugs matches one of the default slugs, but it's not for the same post. We never had a case like that, but maybe that's exactly the reason why @iboved opened this issue in the first place? |
This PR is an alternative version of #586
Instead of creating a separate method that has a different behavior to the original implementation, I managed to fix
transWhere
itself by utilizing 2 queries instead of the slow orWhere clause. This implementation also preserves the fallback functionality.I made some tests on my project (originally discussed here: #581) - now I have 2 queries that execute in ~ 5 ms instead of a single 60 second query. The improvement is significant.
Comments are welcome.