简体   繁体   English

使用DECODE在更新语句中进行性能调整

[英]Performance tuning in update statement with DECODE

I have below update statement which include a reference to a table and a DECODE statement. 我在下面的update语句中包含对表的引用和DECODE语句。 My table has around 500000 records which will alter by this statement and it is taking around 2 1/2 hours to finish. 我的表大约有500000条记录,该记录将被此语句更改,大约需要2 1/2小时才能完成。 Any types of suggestions are welcome to enhance the performance of this. 欢迎任何类型的建议以提高其性能。

Here comes my update statement 这是我的更新声明

   UPDATE TABlE_1 t1
  SET t1.column_1= DECODE( (SELECT creator 
                            FROM table_2 t2
                            WHERE t2.key1 = t1.key1 
                            AND   t2.key2 = t1.key2), 
                            'AAAAAAA_table2', 
                            'aaaaa_table1', 
                            'BBBBBBB_table2', 
                            'bbbbb_table1', 
                            'ccccc_table1')
  WHERE t1.column_1 IS NULL;

If the number of options in the decode statement is not too large, you may be better off running a number of targeted update statements. 如果解码语句中的选项数量不是太大,则最好运行一些目标更新语句。

eg 例如

    UPDATE table_1 t1 
    SET t1.column_1 = 'aaaaa_table_1' 
    WHERE EXISTS (SELECT 1 
                 FROM table_2 t2
                 WHERE t2.key1 = t1.key1 
                 AND   t2.key2 = t1.key2
                 AND creator = 'AAAAAA') 
   AND t1.column_1 IS NULL

And then repeat this for each mapping you are trying to perform. 然后针对您要执行的每个映射重复此操作。

You seem to have a rule that transforms the CREATOR value when inserting to TABLE_1. 您似乎有一条规则,可以在插入TABLE_1时转换CREATOR值。 How about just applying the rule. 仅应用规则呢。

UPDATE table_1 t1
   SET t1.column_1 = (SELECT LOWER(SUBSTR(creator,1,5))||'_table1' 
                        FROM table_2 t2
                       WHERE t2.key1 = t1.key1 
                         AND t2.key2 = t1.key2)
  WHERE t1.column_1 IS NULL

Or use a merge statement 或使用合并语句

MERGE INTO table_1 t1
USING (SELECT LOWER(SUBSTR(creator,1,5))||'_table1' AS derived_creator
         FROM table_2) t2  
       ON (    t2.key1 = t1.key1 
           AND t2.key2 = t1.key2)
WHEN MATCHED THEN UPDATE SET t1.column_1 = t2.derived_creator

I haven't run these on a dataset so can't say that either would be quicker. 我没有在数据集上运行它们,所以不能说两者都更快。 However, I think that they both look neater. 但是,我认为它们看起来都更整洁。 As an aside it is recommended that the CASE expression be used in SQL now instead of the DECODE function which would give you the SQL below. 另外,建议立即在SQL中使用CASE表达式,而不要使用DECODE函数,该函数将为您提供以下SQL。

UPDATE TABlE_1 t1
   SET t1.column_1= SELECT CASE creator 
                           WHEN 'AAAAAAA_table2' THEN 'aaaaa_table1'
                           WHEN 'BBBBBBB_table2' THEN 'bbbbb_table1'
                           ELSE 'ccccc_table1'
                           END                           
                      FROM table_2 t2
                     WHERE t2.key1 = t1.key1 
                       AND t2.key2 = t1.key2
  WHERE t1.column_1 IS NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM