我正在尝试优化以下查询:
SELECT idnum AS ID,
(SELECT stddev(close)
from hist.eurusd1
where idnum between ID-7 AND ID) AS Sdev
FROM hist.eurusd1
where idnum between 1001 and 2000;
大约1000行需要 1800秒,我有大约100万行需要处理。
简单STDDEV
查询的执行速度要快得多:
SELECT stddev(close)
from hist.eurusd1
where idnum between 1003 AND 1010;
对于获取部分,首次为0.047秒,因此1000次查询将为47秒而不是1800秒。
我认为子查询中的某些内容会使整个第一个查询变慢,但不知道如何优化它。
答案 0 :(得分:0)
我意识到这不是一个答案,因为我无法解释为什么给定的子查询如此缓慢。 但我确实知道如何在大约30秒内计算百万行,所以我想发布代码:
SELECT a.idnum as id, stddev(b.close)
from eurusd1 as a
JOIN (
select idnum+7 as ix, close from eurusd1
UNION ALL
select idnum+6 as ix, close from eurusd1
UNION ALL
select idnum+5 as ix, close from eurusd1
UNION ALL
select idnum+4 as ix, close from eurusd1
UNION ALL
select idnum+3 as ix, close from eurusd1
UNION ALL
select idnum+2 as ix, close from eurusd1
UNION ALL
select idnum+1 as ix, close from eurusd1
UNION ALL
select idnum as ix, close from eurusd1
) as b on a.idnum = b.ix
group by a.idnum
答案 1 :(得分:0)
可以接近MariaDB 10.2 Window functions。
我没有看到STD,但AVG会像:
AVG(close) OVER(ORDER BY idnum
RANGE BETWEEN 6 PRECEDING
AND CURRENT ROW)
AS MovingAvg