更新大量行

时间:2016-06-29 18:44:12

标签: sql oracle performance sql-update

我有两个表TESTING和TESTING_BCK,两个都有相同的列。 例如。

测试

NR | KEY1 | KEY2 | ... | KEY7 | COMMENTS
 1  |  ...                     | example1
 1  |  ...                     | example2
 ..... and thousands with NR = 1 more
 2  |  ...                     |  null
 2  |  ...                     |  null
 ..... and again many many more  

TESTING_BCK

NR | KEY1 | KEY2 | ... | KEY7 | COMMENTS
 2  |  ...                     |  example
 2  |  ...                     |  'this should go to TESTING'
 ..... and again many many more 

现在我想更新TESTING,因此TESTING_BCK的评论也将是TESTING的评论(基于密钥)。我知道TESTING_BCK中的所有条目肯定在TESTING中都有相同的键和NR。 到目前为止,我这样做了:

UPDATE TESTING
SET COMMENTS = (
        SELECT COMMENTS
        FROM TESTING_BCK
        WHERE TESTING_BCK.NR = TESTING.NR
            AND TESTING_BCK.KEY1 = TESTING.KEY1.....TESTING_BCK.KEY7 = TESTING.KEY7
        )
WHERE EXISTS (
        SELECT COMMENTS
        FROM TESTING_BCK
        WHERE TESTING_BCK.NR = TESTING.NR
            AND TESTING_BCK.KEY1 = TESTING.KEY1
            AND...
        )

但是,当我用数千个条目进行此操作时,这需要永远。有什么想法我怎么能更新TESTING呢?

结果应该是:

测试

    NR | KEY1 | KEY2 | ... | KEY7 | COMMENTS
     1  |  ...                     | example1
     1  |  ...                     | example2
     ..... and thousands with NR = 1 more
     2  |  ...                     |  example
     2  |  ...                     |  'this should go to TESTING'
     2  |  ...                     |  comment from TESTING_BCK
     ......

1 个答案:

答案 0 :(得分:1)

我认为你应该使用

UPDATE 
(SELECT TESTING.COMMENTS as OLD, TESTING_BCK.COMMENTS as NEW
 FROM TESTING
 INNER JOIN TESTING_BCK
 ON TESTING_BCK.NR = TESTING.NR AND TESTING_BCK.KEY1 = TESTING.KEY1 ..... TESTING_BCK.KEY7 = TESTING.KEY7
) t
SET t.OLD = t.NEW