私は、次の単純化されたクエリがある:MySQLの再利用合計
Select
(select sum(f1) from tableA a where a.id = t.id) sum1,
(select sum(f2) from tableB b where b.id = t.id) sum2,
t.*
from Table t;
私の願いは、SUM1とSUM2はそれらを再度計算することなく再利用することです。もちろん、iの
Select
(select sum(f1) from tableA a where a.id = t.id) sum1,
(select sum(f2) from tableB b where b.id = t.id) sum2,
sum1 + sum2 `sum3`,
t.*
from Table t;
次のクエリを実行できますが、これは実行時間を2倍にする必要はありません。
Select
(select sum(f1) from tableA a where a.id = t.id) sum1,
(select sum(f2) from tableB b where b.id = t.id) sum2,
(select sum(f1) from tableA a where a.id = t.id) +
(select sum(f2) from tableB b where b.id = t.id) `sum3`,
t.*
from Table t;
さらにはsum1とsum2 res私は一時的なテーブルにultsがイメージング私は何かを見落としていることができないmysqlは、いくつかの効率的なクエリを集計フィールドで行う。
合計フィールドを再利用する方が、より効率的で効率的な方法はありますか?