0

我正在尝试更新 Oracle 中的多个列,并在经历了一些场景后解决了下面的代码。

我的问题是,更精简的代码不会更新我需要的内容,而更复杂的代码会更新——我正在尝试通过最小的处理能力来实现这一点,因为我正在查看数十万甚至更多的更新。

所以第一个代码:

UPDATE table@database1 i2 
SET    (i2.access, i2.permission) = 
(select 
i2m.access, i2m.permission 
from temporarytable ss
    JOIN table2 pgm 
     ON ss.secgroup = pgm.string 
   JOIN table i2m 
     ON pgm.hmy = i2m.hgroup 
        AND ss.pername = i2m.sobjname 
   JOIN table2@database1 pg 
     ON ss.secgroup = pg.string 

    WHERE 
    pg.string = 'string'  -- this limits the updates to a specific subset of data
    and i2m.hmy > 0 -- this for some freak records in both tables that are missing a primary key
    and pg.hmy = i2.hgroup -- this matches the key of the 'string' from above to the records i need to update
    and ss.pername = i2.sobjname -- further condition on which records to update. so only the ones that match from the temp table to the target table
    and ss.orig_hmy = i2.hmy) -- further condition to make sure i am updating only records matching between temp table and target table

现在,如果我运行它,而不是只更新大约 700 条与上述子查询匹配的记录,它会更新表“table@database1”中的所有记录,我不明白为什么(可能是我不明白的事情之一甲骨文 :) )

但是,如果我运行以下命令——唯一的区别是我将整个子查询插入到“存在的地方”中——那么这只会更新我需要的内容。我的问题是,按照我的理解,子查询运行了两次——一次在更新中,一次在 where 子句中——我会说这是对处理能力的浪费。

UPDATE table@database1 i2 
SET    (i2.access, i2.permission) = 
(select 
i2m.access, i2m.permission 
from temporarytable ss
    JOIN table2 pgm 
     ON ss.secgroup = pgm.string 
   JOIN table i2m 
     ON pgm.hmy = i2m.hgroup 
        AND ss.pername = i2m.sobjname 
   JOIN table2@database1 pg 
     ON ss.secgroup = pg.string 

    WHERE 
    pg.string = 'string'
    and i2m.hmy > 0
    and pg.hmy = i2.hgroup
    and ss.pername = i2.sobjname
    and ss.orig_hmy = i2.hmy)

where exists (select 
i2m.access, i2m.permission 
from temporarytable ss
    JOIN table2 pgm 
     ON ss.secgroup = pgm.string 
   JOIN table i2m 
     ON pgm.hmy = i2m.hgroup 
        AND ss.pername = i2m.sobjname 
   JOIN table2@database1 pg 
     ON ss.secgroup = pg.string 

    WHERE 
    pg.string = 'string'
    and i2m.hmy > 0
    and pg.hmy = i2.hgroup
    and ss.pername = i2.sobjname
    and ss.orig_hmy = i2.hmy)

注意:如果它没有显示,我有多个具有相同架构的数据库。我正在尝试使用来自主模式的信息更新跨数据库的表。Temp 表充当不同且需要更新的记录的存储库 - 如果仅 15% 与主模式不同,则没有理由更新数百万条记录。

4

1 回答 1

1

在听取了所有乐于助人的人的建议后,我研究了使用 MERGE,上面的查询被改编成下面的 - 事实证明是成功的!

    MERGE INTO table@database1 i2 
    USING (
    select i2m.access, i2m.permission, ss.orig_hmy
    from table i2m
        JOIN table2 pgm 
         ON i2m.hgroup = pgm.hmy 
       JOIN temporarytable ss
         ON pgm.string = ss.string 
            AND ss.pername = i2m.sobjname 
       JOIN table2@database1 pg 
         ON ss.string = pg.string 
    WHERE 1 = 1
        and i2m.hmy > 0 
        and pg.string = 'string'
        and ss.database = 'database1' 
    ) u on (i2.hmy = u.orig_hmy)
    WHEN MATCHED THEN
        UPDATE SET i2.access = u.access, i2.permission = u.permission;

非常感谢大家!

于 2016-07-29T01:04:14.510 回答