Answer the question
In order to leave comments, you need to log in
Laravel. Why is there a hundreds of times difference in time requests?
If I do this:
\DB::table('ratings')
->selectSub(function ($query) {
return $query->selectRaw('SUM(ecology)');
}, 'ecology')
->selectSub(function ($query) {
return $query->selectRaw('SUM(social_protection)');
}, 'social_protection')
->where('point', 'test')
->first();
select (select SUM(ecology)) as `ecology`,
(select SUM(social_protection)) as `social_protection`
from `ratings` where `point` = ? limit 1
\DB::select('select (select SUM(ecology)) as `ecology`,
(select SUM(social_protection)) as `social_protection`
from `ratings` where `point` = ? limit 1', ['test'])
select (select SUM(ecology)) as `ecology`,
(select SUM(social_protection)) as `social_protection`
from `ratings` where `point` = ? limit 1
Answer the question
In order to leave comments, you need to log in
The fact is that when you pass a number instead of a string, an implicit call to CAST occurs .
Most likely your point is a string type and therefore CAST() is performed for each line, which significantly increases the code execution time.
I will add a description of your scenario
When comparing a string column to a number, MySQL cannot use the index to look up the value.
The stupidest question, because there is no point in using pure SQL, so it would be possible to score. I subscribe under the answer Nurlan
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question