zoukankan      html  css  js  c++  java
  • 使用开窗函数优化查询的一个案例

    早上检查报警邮件时发现又是1000+的报警,于是查找凶手...
    最终找到了罪魁祸首,一个ETL查询从晚上10点开始跑到凌晨1点50,好家伙足足跑了3小时50分钟,阻塞了一片一片的JOB:

    SELECT 
     ****
    FROM 
     A
    INNER JOIN
     (
      SELECT XXX,A.END_DATE_REP,MAX(PUBLISH_DATE) AS PUBLISH_DATE
      FROM A
      INNER JOIN 
       (
        SELECT XXX,MAX(END_DATE_REP) AS END_DATE_REP
        FROM A
        GROUP BY XXX
       )B
      ON A.XXX = B.XXX
      AND A.END_DATE_REP=B.END_DATE_REP
      GROUP BY A.XXX,A.END_DATE_REP
     ) C
    ON A.XXX = C.XXX
    AND A.END_DATE_REP=C.END_DATE_REP
    AND A.PUBLISH_DATE=C.PUBLISH_DATE
    LEFT JOIN (SELECT A.* 
        FROM B
        JOIN 
      (
       SELECT XXX,FISCAL_PERIOD,MAX(PUBLISH_DATE) AS  PUBLISH_DATE
       FROM  B
       GROUP BY XXX,FISCAL_PERIOD
      ) D
        ON B.XXX = D.XXX
        AND B.PUBLISH_DATE = D.PUBLISH_DATE
        ) F
    ON A.XXX = F.XXX
    AND A.END_DATE_REP = F.END_DATE_REP

    一看到这个查询,瞬间被石化了。典型的使用开窗函数的场景嘛。尝试使用如下的开窗函数写法后,妥妥的10s内解决战斗。

    SELECT 
     ****
    FROM
    (
    SELECT
     XXX,RANK() OVER (PARTITION BY XXX ORDER BY END_DATE_REP DESC,PUBLISH_DATE DESC) RAK
    FROM A WITH(NOLOCK)
    ) C
    LEFT JOIN
    (
    SELECT 
     XXX,END_DATE_REP,RANK() OVER (PARTITION BY XXX,END_DATE_REP ORDER BY PUBLISH_DATE DESC) RAK
    FROM  B WITH(NOLOCK)
    ) F
    ON C.XXX=F.XXX
    AND C.END_DATE_REP = F.END_DATE_REP
    AND F.RAK = 1 
    WHERE C.RAK = 1
  • 相关阅读:
    luogu1210 回文检测
    luogu2420 让我们异或吧
    luogu4151 最大XOR和路径
    线性基
    博弈论(扯淡)
    矩阵求逆 模板
    luogu2513 逆序对数列
    洛谷4316 绿豆蛙的归宿(DAG递推/概率dp)
    1898: [Zjoi2005]Swamp 沼泽鳄鱼
    矩阵
  • 原文地址:https://www.cnblogs.com/ajiangg/p/4742831.html
Copyright © 2011-2022 走看看