递归cte表-计算ewma(ema)-如何优化/重构此代码,以免每次都被tdwm杀死?

apeeds0o  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(329)

我创建了一个查询来生成cte表,两个是非递归的,一个是递归的,以便计算指数加权移动平均(ema)。当我在teradata中运行代码时,一段时间后它就会被tdwm杀死。
有什么办法来改进/解决这个问题吗?

WITH 

smooth AS (
    SELECT CAST(0.741870935 AS NUMERIC (20,5)) AS alpha
),

numbered AS (
    SELECT 
        ROW_NUMBER() OVER (ORDER BY customer_name, closed_date) as rn,  
        customer, 
        closed_date, 
        metric

    FROM my_table
),

recursive EWMA AS (
    SELECT rn, customer, closed_date, metric, CAST(metric AS NUMERIC(20,5)) as EWMA
    FROM numbered
    WHERE rn = 1

    UNION ALL

    SELECT numbered.rn, numbered.customer, numbered.closed_date, numbered.metric,
    smooth.alpha * numbered.metric + (1-smooth.alpha) * EWMA.EWMA
    FROM EWMA
    JOIN numbered
    ON EWMA.rn + 1 = numbered.rn
    CROSS JOIN smooth   
)

SELECT * FROM EWMA
ORDER BY closed_date;
uinbv5nw

uinbv5nw1#

你试过设定一个 depth 字段来限制递归?比如:

WITH smooth AS (...),
numbered AS (...),
recursive EWMA AS (
  SELECT 
    rn, customer, closed_date, metric, CAST(metric AS NUMERIC(20,5)) as EWMA, 
    1 AS depth
  FROM numbered
  WHERE rn = 1

  UNION ALL

  SELECT 
    numbered.rn, numbered.customer, numbered.closed_date, numbered.metric,
    smooth.alpha * numbered.metric + (1-smooth.alpha) * EWMA.EWMA, 
    EWMA.Depth + 1 AS Depth
  FROM EWMA
  INNER JOIN numbered ON EWMA.rn + 1 = numbered.rn
  CROSS JOIN smooth   
  WHERE depth <= 10 -- Restrict recursion
)
SELECT * 
FROM EWMA
ORDER BY closed_date;

递归连接 numbered 可能是问题的根源,假设 my_table table很大。理想情况下,您应该在pi列上执行一个直接的相等联接——即。 table1.pi_col1 = table2.pi_col2 . 不知道如何使用 +1 表达式将影响连接。
从较高的层次来看,您似乎只想在当前行的计算中使用前一行的值。如果是这样的话,那么您可以完全取消递归cte,只使用 LAG() 窗口功能:

WITH smooth AS (
  SELECT CAST(0.741870935 AS NUMERIC (20,5)) AS alpha
)
SELECT 
  ROW_NUMBER() OVER (ORDER BY customer_name, closed_date) as rn, -- row number
  customer, 
  closed_date, 
  metric,
  CAST(
    COALESCE(
      (smooth.alpha * metric + (1-smooth.alpha)) * -- current row's value
      LAG((smooth.alpha * metric + (1-smooth.alpha))) OVER(
        ORDER BY customer_name, closed_date) -- previous row's value
      , metric -- handle first row (no previous "EWMA" value)
    )
  AS NUMERIC(20,5)) AS EWMA
FROM my_table;

相关问题