MySQL查询优化,用于计算2条记录之间的周期性回报率

时间:2016-09-24 03:12:52

标签: mysql sql performance

我开始工作的以下查询计算两个记录之间的定期回报率。我无法弄清楚如何编写这个逻辑,因此它会在给定的自动收报机的整个表中运行。任何指导都将不胜感激。

SELECT 
   (SELECT closeprice FROM marketdata.nyse
    WHERE ticker = 'DDD' and
    eoddate < 20150113 order by eoddate desc limit 1) as `cp_t1`
,closeprice as `cp_t0`
, log(
        closeprice /
        (SELECT closeprice FROM marketdata.nyse
            WHERE ticker = 'DDD' and
            eoddate < 20150113 order by eoddate desc limit 1) 
        ) as PRoR
FROM marketdata.nyse
WHERE ticker = 'DDD' and
eoddate = 20150113;

它正在回归我想要的和期望的。

cp_t1               cp_t0   PRoR
31.360000610351562  30.88   0.02392378745852884

我的表格设计如下。

tick_id, name, ticker, eoddate, openprice, highprice, lowprice, closeprice, volume, pror, prorpct
1, 3D Systems Corp, DDD, 2015-01-01, 32.87, 32.87, 32.87, 32.87, 0, , 
2, 3D Systems Corp, DDD, 2015-01-02, 32.98, 33.97, 31.89, 32.42, 2571900, , 
3, 3D Systems Corp, DDD, 2015-01-05, 31.99, 32, 30.3, 30.74, 2890500, , 
4, 3D Systems Corp, DDD, 2015-01-06, 30.85, 31.38, 29.26, 30.15, 2967500, , 
5, 3D Systems Corp, DDD, 2015-01-07, 30.4, 31.22, 30.2, 30.88, 2145000, , 
6, 3D Systems Corp, DDD, 2015-01-08, 31.37, 32.73, 30.83, 32.32, 2988300, , 

1 个答案:

答案 0 :(得分:0)

我必须学习如何编写存储过程来实现我的目标。代码如下:

CREATE DEFINER=`ericg`@`%` PROCEDURE `calcPRoR`(IN symbol varchar(7))
BEGIN

DECLARE _output TEXT DEFAULT '';
DECLARE done, inner_done INT DEFAULT 0;
DECLARE c_tick_id INT;
DECLARE c_name VARCHAR(75);
DECLARE c_ticker VARCHAR(7);
DECLARE c_eoddate DATE;
DECLARE c_openprice, c_highprice, c_lowprice, c_closeprice, c_inner_closeprice, c_pror, c_prorpct FLOAT;
DECLARE c_volume INT(18);
DECLARE cursor_var CURSOR FOR SELECT tick_id, name, ticker, eoddate, openprice, highprice, lowprice, closeprice, volume FROM marketdata.marketdata WHERE ticker = symbol;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cursor_var;

cursor_var_loop: LOOP
 FETCH cursor_var INTO c_tick_id, c_name, c_ticker, c_eoddate, c_openprice, c_highprice, c_lowprice, c_closeprice, c_volume;


 -- cursor loop statements
 INNER_BLOCK: BEGIN
    DECLARE inner_cursor_var CURSOR FOR SELECT closeprice FROM marketdata.marketdata WHERE ticker = c_ticker and eoddate < c_eoddate order by eoddate desc limit 1;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET inner_done = 1;

    OPEN inner_cursor_var;

    inner_cursor_var_loop: LOOP
        FETCH inner_cursor_var INTO c_inner_closeprice;

        -- inner cursor statements
        SET c_pror      := c_closeprice - c_inner_closeprice;
        SET c_prorpct   := log(c_closeprice / c_inner_closeprice);

        -- output to screen for debugging
        SET _output     = CONCAT(c_eoddate,", ",c_closeprice,", ",c_inner_closeprice,", ",c_pror,", ",c_prorpct);
        SELECT _output;

        IF (inner_done = 1) THEN
            LEAVE inner_cursor_var_loop;

        END IF;
    END LOOP inner_cursor_var_loop;

    CLOSE inner_cursor_var;
 END INNER_BLOCK;   


 IF (done = 1) THEN
        LEAVE cursor_var_loop;
 END IF;
END LOOP;

CLOSE cursor_var;

END