List:General Discussion« Previous MessageNext Message »
From:Shawn Green Date:January 19 2010 8:42pm
Subject:Re: Subquery performance slow to non-existent...
View as plain text  
Nick Torenvliet wrote:
> Hey all...
> 
> I am having a problem with sub-queries that I cannot trouble shoot.
> 
> I run query a:
> select symbol from names where market like 'NYMEX' and name like 'natural
> gas {%';
> 
> and get  168 names that I manually insert into query b:
>  select * from endOfDayData where endOfDayData.market like 'NYMEX' and
> endOfDayData.symbol IN
> (... big list clipped ...);
> 
> Running query b gives me a result set as follows:
> 
> | 2010-01-15 | NYMEX  | NGZ22  |  8.9620 |  8.9680 |  8.9620 |  8.9680
> |      0 |
> +------------+--------+--------+---------+---------+---------+---------+--------+
> 86765 rows in set (4.46 sec)
> 
> I then because I want to generalize query b I continue by creating query c
> as follows:
> mysql> select * from endOfDayData where endOfDayData.market like 'NYMEX'
> and  endOfDayData.symbol IN (select names.symbol from names where
> names.market like  'NYMEX' and names.name like 'natural gas {%');
> 
> Query c seems to have good syntax as neither the command line mysql
> interface nor the gui spit it back but it literally takes forever to run;
> I've waited at least twenty minutes and not got anything back.  I'm running
> Ubuntu 9.10 on an intel core i7 with 4GB RAM and 12GB swap... the process
> monitor doesn't even flinch so I'm not thinking hardware here... why is the
> sub-query running so slow?
> 
> Thanks for you help!!

Subqueries are also not indexed. Even if this is an independent 
subquery, the optimizer will still need to scan the results of each 
subquery for every line of the outer query.

Try rewriting this as a joinl

SELECT eod.*
from endOfDayData eod
INNER JOIN names n
   on n.symbol = eod.symbol
   and n.market like 'NYMEX'
   and names.name like 'natural gas {%';

Try that and see what a difference it makes.

-- 
Shawn Green, MySQL Senior Support Engineer
Sun Microsystems, Inc.
Office: Blountville, TN


Thread
Subquery performance slow to non-existent...Nick Torenvliet19 Jan
  • Re: Subquery performance slow to non-existent...Dan Nelson19 Jan
  • Re: Subquery performance slow to non-existent...Shawn Green19 Jan
  • Re: Subquery performance slow to non-existent...Peter Brawley19 Jan