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 43: Which indexes would you create to speed up the query below? – MySQL Question of the Day

Skip to content

By plogi in mysql questions

See the following scenario:

create table jgen
  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 j1tst
  select floor(rand()*10) + 1 as jttype,
    adddate('2010-01-01', interval rand()*360 day) as jdate,
    (rand()*1000000/100) as jtsum
  from jgen a,jgen b,jgen c,jgen d,jgen e,jgen f;
create table j2tst
  select n jttype,uuid() as jtname from jgen a;

 

Which indexes would you create to speed up the query below?
(Find all)
 

select count(*) from j1tst v1
  inner join j2tst v2 on (v1.jttype=v2.jttype)
  where jdate between '2010-01-01' and '2010-01-10';

 

a) alter table j1tst add index (jdate);
b) alter table j1tst add index (jttype);
c) alter table j2tst add index (jttype);
d) alter table j2tst add index (jtname);
e) alter table j1tst add index (jtsum);

 
[ Basic Optimizations (10%) - Using Indexes for Optimization ]

Tags: , ,

Comment Feed

One Response

  1. plogi18. May 2010 @ 16:37:11


    a c

    The index on jdate will restrict the rows from j1tst and the index on j2tst.jttype will support the join.
    Group by with columns from both tables can prevent the use of indexes and change the plan.
    Try explaining the query below:

    select jdate,jtname,sum(jtsum) from j1tst v1
      inner join j2tst v2 on (v1.jttype=v2.jttype)
      where jdate between '2010-01-01' and '2010-01-10'
      group by jdate,jtname;

You must be logged in to post a comment.