簡體   English   中英

從 select 更新/創建表,並更改 Oracle 11g 中的列值(加速)

[英]Update/Create table from select with changes in the column values in Oracle 11g (speed up)

在工作中,我們有一些 Oracle 11g 數據庫的更新腳本,大約需要 20 小時,一些最苛刻的查詢是我們更改一些值的更新,例如:

UPDATE table1 SET
    column1 = DECODE(table1.column1,null,null,'no info','no info','default value'),
    column2 = DECODE(table1.column2,null,null,'no info','no info','another default value'),
    column3 = 'default value';

像這樣,我們有很多更新。 問題是這些表有大約 1000 萬行。 我們還有一些更新,其中某些列將具有默認值但它們可以為空(我知道它們是否具有非 null 和默認約束,那么這些列的添加幾乎是立即的,因為這些值在目錄中),然后更新或添加此類列會花費大量時間。

我的方法是重新創建表(正如 TOM 在https://asktom.oracle.com/pls/asktom/f?p=100:11:0::NO::P11_QUESTION12330:6407993 中所說 但是我不知道如何從原始表中檢索一些列,這些列將保持不變,而其他列將更改為默認值(並且在更新之前,此類列具有合理的信息),這是因為我們需要對一些信息保密。

所以,我的方法是這樣的:


CREATE TABLE table1_tmp PARALLEL NOLOGGING
    AS (select col1,col2,col3,col4 from table1);

ALTER TABLE table1_tmp ADD ( col5 VARCHAR(10) default('some info') NOT NULL;
ALTER TABLE table1_tmp ADD ( col6 VARCHAR(10) default('some info') NOT NULL;

ALTER TABLE table1_tmp ADD ( col7 VARCHAR(10);
ALTER TABLE table1_tmp ADD ( col8 VARCHAR(10);
MERGE INTO table1_tmp tt
    USING table1 t
    ON (t.col1 = tt.col1)
WHEN MATCHED THEN
    UPDATE SET
        tt.col7 = 'some defaul value that may be null',
        tt.col7 = 'some value that may be null';

我還嘗試創建可空值而不是 null 來快速完成它,並且工作正常,問題是當我將列返回到 null 時,該操作需要太多時間。 最后一個代碼最終也消耗了大量時間(合並超過一個小時)。

希望對如何提高這樣的東西的性能有一個想法。 提前致謝!

也許您可以在加入合並時嘗試使用 NVL:

MERGE INTO table1_tmp tt
    USING table1 t
    ON (nlv(t.col1,'-3') = nvl(tt.col1,'-3'))
WHEN MATCHED THEN ....

如果您不想更新 null 值,您也可以這樣做:

MERGE INTO table1_tmp tt
    USING table1 t
    ON (nlv(t.col1,'-3') = nvl(tt.col1,'-2'))
WHEN MATCHED THEN .....

最后,我使用原始表中的數據創建了一個臨時表,並在創建時插入默認值和解碼以及任何其他內容,比如如果我想將某些內容設置為 NULL,我做了演員表。 就像是:

    CREATE TABLE table1_tmp AS (
        column1 default "default message",
        column2, --This column with no change at all
        column3, --This will take the value from the decode below 
     ) AS SELECT 
        "default message" column1,
        column2 --This column with no change at all,
        decode(column3, "Something", NULL, "A", "B") column3,
     FROM table1;

這就是我解決問題的方法。 處理一個 2300 萬行的表大約需要 3 到 5 分鍾,而過去更新需要幾個小時。 現在只需要設置權限、約束、索引、評論,僅此而已,但這些東西只需要幾秒鍾。

感謝@thehazal 的回答無法檢查您的方法,但聽起來很有趣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM