oracle 优化使用MAX()的更新查询

lsmd5eda  于 2023-08-03  发布在  Oracle
关注(0)|答案(2)|浏览(123)

这是针对Oracle 19 DB的问题。我需要一些帮助来优化此更新语句:

UPDATE T1 T1
   SET T1.COL1 = (SELECT MAX(T2.COL1)
                   FROM T2 T2
                   WHERE T2.COL2 = T1.COL2)
 WHERE T1.COL2 BETWEEN :1 AND :2
   AND T1.COL1 IS NULL
   AND EXISTS
 (SELECT 1
          FROM (SELECT MAX(T2.COL1) 
                  FROM T2 T2
                 WHERE T2.COL2 = T1.COL2) V1
         WHERE V1.COL1 IS NOT NULL)

字符串
我喜欢这个答案:
Massive UPDATE vs. MERGE performance on Oracle
我尝试做类似的,但我的问题是,我的更新有MAX(),我不知道如何优化它。

arknldoa

arknldoa1#

Merge,正如你所说:

merge into t1 
  using (select t2.col2, 
                max(t2.col1) max_col1
         from t2
         group by t2.col2
         having max(t2.col1) is not null
        ) x
  on (t1.col2 = x.col2)
  when matched then update set
    t1.col1 = x.max_col1
  where t1.col2 between :1 and :2
    and t1.col1 is null;

字符串

7fyelxc5

7fyelxc52#

使用带有HAVING子句的MERGE语句执行V1.COL1 IS NOT NULL过滤器(或者,在GROUP BY之前使用WHERE子句):

MERGE INTO T1
USING (
  SELECT col2,
         MAX(col1) AS col1
  FROM   t2
  -- WHERE col1 IS NOT NULL
  GROUP BY col2
  HAVING MAX(col1) IS NOT NULL
) t2
ON (t1.col2 = t2.col2)
WHEN MATCHED THEN
  SET   T1.COL1 = t2.col2
  WHERE T1.COL2 BETWEEN :1 AND :2
  AND   T1.COL1 IS NULL;

字符串

相关问题