View Single Post

   
  #1 (permalink)  
Old 02-28-2008, 11:29 AM
Ted
 
Posts: n/a
Default How can I improve the performance of this kind of query?

Please consider the following stored procedure:

DROP PROCEDURE IF EXISTS `get_etf_stocks_and_prices`;
CREATE PROCEDURE `get_etf_stocks_and_prices`(id INTEGER)
BEGIN
SELECT stocks.stock_id AS stock_id,
stocks.symbol AS symbol,
stocks.name AS stock_name,
A.`close` AS close_price,
A.price_date AS price_date
FROM etf_stocks LEFT JOIN stocks ON etf_stocks.stock_id =
stocks.stock_id
LEFT JOIN stockprices A ON stocks.stock_id = A.stock_id
LEFT JOIN stockprices B ON A.stock_id = B.stock_id
AND B.price_date > A.price_date
WHERE B.price_date IS NULL
AND etf_id = id;
END
//

While price_date is part of the primary key, creating an extra index
on it dramatically improved how long it takes to complete. However,
it takes close to 10 minutes for the results to be displayed where
there are 28 stocks in the etf required (out of almost 500 possible
stocks for almost 500 etfs), the times shown by MySQL Query browser
are 0.0037s (520.3263s): the latter s almost the 20 minutes I
experience waiting for the results to be displayed. Given that there
is daily data going back over 40 years, at least for some stocks, I
don't want to think how long it would take to get the final close
price for each week represented in the database for the stocks
requested.

The data I get appear to be correct, but I need to get it MUCH much
more quickly.

Thanks

Ted

Reply With Quote