for simple, I write an example like this
select
a.xx,
a.xx,
... -- all table a's field
b.date,
b.money
c.date,
c.money,
d.date,
d.money,
e.date,
e.money
from
a
left join b on a.id = b.a_id
left join c on a.id = c.a_id
left join d on a.id = d.a_id
left join e on a.id = e.a_id
where ... -- some condition
limit 10
In this sql, table b
and c
is one to one to table a
but table d
and e
is not that.
With no optimization. table d
and e
while seem like this
-- ... sql before
left join
(
select * from
(select * from d order by date desc) as temp
group by a_id
) as d on a.id = d.a_id
left join
(
select * from
(select * from e order by id desc) as temp
group by a_id
) as e on a.id = e.a_id
-- ... sql after
But in fact, it is very slow when running this.
table d
was a new table, it didn't has larges of data yet(it will be large in future).
table e
had million now. subquery of table e
make my sql very slow. How to optimization it?