See the following situation:
create table fgen 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 f1tst select floor(rand()*10) + 1 as ftype, adddate('2010-01-01', interval rand()*360 day) as fdate, (rand()*1000000/100) as fsum from fgen a,fgen b,fgen c,fgen d,fgen e; alter table f1tst add index (fdate); explain select min(fdate),max(fdate),sum(fsum) from f1tst v1 where subdate(fdate,interval 10 day) >= now();
Which of the following plans belongs to the query?
a)
+--+-----------+---+-----+--------+-----+-------+----+----+-----------+
|id|select_type|tab| type|possible| key |key_len| ref|rows| Extra |
+--+-----------+---+-----+--------+-----+-------+----+----+-----------+
| 1| SIMPLE | v1|range| fdate |fdate| 9 |NULL|2663|Using where|
+--+-----------+---+-----+--------+-----+-------+----+----+-----------+
b)
+--+-----------+---+----+--------+----+-------+----+------+-----------+
|id|select_type|tab|type|possible| key|key_len| ref| rows | Extra |
+--+-----------+---+----+--------+----+-------+----+------+-----------+
| 1| SIMPLE | v1| ALL| fdate |NULL| NULL |NULL|100000|Using where|
+--+-----------+---+----+--------+----+-------+----+------+-----------+
c)
+--+-----------+---+----+--------+----+-------+----+------+-----------+
|id|select_type|tab|type|possible| key|key_len| ref| rows | Extra |
+--+-----------+---+----+--------+----+-------+----+------+-----------+
| 1| SIMPLE | v1| ALL| NULL |NULL| NULL |NULL|100000|Using where|
+--+-----------+---+----+--------+----+-------+----+------+-----------+
d)
+--+-----------+----+----+--------+----+-------+----+----+----------------+
|id|select_type| tab|type|possible| key|key_len| ref|rows| Extra |
+--+-----------+----+----+--------+----+-------+----+----+----------------+
| 1| SIMPLE |NULL|NULL| NULL |NULL| NULL |NULL|NULL|Impossible WHERE|
+--+-----------+----+----+--------+----+-------+----+----+----------------+
[ Basic Optimizations (10%) - Overview of Optimization Principles ]
c
Anwers a and b show possible keys, but in the query the subdate around fdate actually prevents index usage.
d is the plan for: