Если делаю так:
\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();
Результат мгновенно
"time" => 2.0
Сам SQL получается такой:
select (select SUM(ecology)) as `ecology`,
(select SUM(social_protection)) as `social_protection`
from `ratings` where `point` = ? limit 1
Но если делаю напрямую через
select:
\DB::select('select (select SUM(ecology)) as `ecology`,
(select SUM(social_protection)) as `social_protection`
from `ratings` where `point` = ? limit 1', ['test'])
То время увеличивается в сотни раз!!!
"time" => 787.87
Хотя запрос SQL такой же:
select (select SUM(ecology)) as `ecology`,
(select SUM(social_protection)) as `social_protection`
from `ratings` where `point` = ? limit 1
Почему такая разница?