Have a look at the following tables:
create table ggen 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 g1tst select floor(rand()*10) + 1 as gtype, adddate('2010-01-01', interval rand()*360 day) as gdate, (rand()*1000000/100) as gsum from ggen a,ggen b,ggen c,ggen d,ggen e;
Which indexes will help with the performance of the following query?
(Find all correct answers)
select count(*) from g1tst v1,g1tst v2 where v1.gdate=v2.gdate and v1.gtype=1 and v2.gtype=2; +----------+ | count(*) | +----------+ | 278397 | +----------+ 1 row in set (34.37 sec) a) alter table g1tst add index (gdate,gtype); b) alter table g1tst add index (gtype,gdate); c) alter table g1tst add index (gtype); d) alter table g1tst add index (gsum); e) alter table g1tst add index (gdate);
[ Basic Optimizations (10%) - Using Indexes for Optimization ]
a b e
a and b decrease execution time drastically with b being the fastest.
e helps (a bit) but the amount of accessed rows is still huge.
d has no effect.
The selectivity of c is very low with 10 different values – the optimizer will use it, but it actually doesn’t help.
Try to create each index and explain the query!