Check the scenario below:
create table egen select 1 as n union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10; create table e1tst select floor(rand()*10) + 1 as etype, adddate('2010-01-01', interval rand()*360 day) as edate, (rand()*1000000/100) as esum from egen a,egen b,egen c,egen d,egen e; create table e2tst select n etype,uuid() as ename from egen a; alter table e1tst add index (edate,etype); alter table e2tst add index (etype);
+--+------------------+---+-----+--------+-----+-------+----+----+-----------+
|id|select_type |tab|type |possible|key |key_len|ref |rows|Extra |
+--+------------------+---+-----+--------+-----+-------+----+----+-----------+
| 1|PRIMARY | v1|range| edate |edate| 9 |NULL|2426|Using where|
| 2|DEPENDENT SUBQUERY| v2|ref | etype |etype| 8 |func| 2|Using where|
+--+------------------+---+-----+--------+-----+-------+----+----+-----------+
2 rows in set (0.00 sec)
Which query belongs to the execution plan?
a) select edate,ename,sum(esum) from e1tst v1 inner join e2tst v2 on (v1.etype=v2.etype) where edate between '2010-01-01' and '2010-01-10' group by edate,ename; b) select edate, (select v2.ename from e2tst v2 where v1.etype=v2.etype) as ename,sum(esum) from e1tst v1 where edate between '2010-01-01' and '2010-01-10' group by edate,etype; c) select s1.edate,s2.ename,s1.sumesum from (select edate,etype,sum(esum) as sumesum from e1tst v1 where edate between '2010-01-01' and '2010-01-10' group by edate,etype) s1 inner join e2tst s2 on (s1.etype=s2.etype);
[ Basic Optimizations (10%) - Using Indexes for Optimization ]
b is the correct answer