Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993
Question 45: Which of the following plans belongs to the query? – MySQL Question of the Day

Skip to content

By plogi in mysql questions

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 ]

Tags: , ,

Comment Feed

One Response

  1. plogi20. May 2010 @ 19:58:48


    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:

    explain select min(fdate),max(fdate),sum(fsum) from f1tst v1
      where fdate>=subdate(NOW(),interval 10 day) and false;
    

You must be logged in to post a comment.