【问题标题】:Merging multiple sql update statements(Oracle)合并多个sql更新语句(Oracle)
【发布时间】:2017-10-04 10:50:11
【问题描述】:

我不太擅长 sql,我尝试了一些东西。考虑到代码的性能,将这 5 个更新语句组合成一个语句的最佳方法是什么?会有很大的帮助。非常感谢!

代码:

----------------1

Update main_table
set  a = (case
..some code..  end)
where condition_2;

----------------2

Update main_table
set  b = (case
..some code.. end)
where condition_2

----------------3

Update main_table
set  c = (select x from sec_table where conditon_1)
where condition_2

----------------4

Update main_table
set  d = (select y from sec_table where conditon_1)
where condition_2

----------------5

Update main_table
set  e = (select z from sec_table where conditon_1)
where condition_2

【问题讨论】:

  • 我已经尝试过了,但我正在寻找更好的性能:UPDATE main_table SET a = (CASE some code END), b = (CASE some code END), c = (SELECT x FROM sec_table WHERE condition_2), d = (SELECT y FROM sec_table WHERE condition_2), e = (SELECT z FROM sec_table WHERE condition_2) WHERE condition_1;

标签: sql oracle performance


【解决方案1】:

我想你可以这样写:

update main_table
    set a = (case ..some code.. end), 
        b = (case ..some code.. end), 
        (c, d, e) = (select x, y, z from sec_table where conditon_1)
where condition_2

【讨论】:

  • 我试过这个但是 ---- (c, d, e) = (select x, y, z from sec_table where conditon_1)---- 这部分代码给了我一些意想不到的结果.
  • 我认为我在 where 子句中的条件有问题
【解决方案2】:

您可以像这样组合更新查询并仅使用一个查询:

UPDATE main_table
   SET a = (case ..some code.. end) ,
       b = (case ..some code.. end) ... /*the rest of your sets*/
   where /*add your conditions*/

【讨论】:

    【解决方案3】:

    您只能在一个更新语句上执行此操作。根据您对该 sec_table 进行的子查询,您甚至可以进行更多调整。

    update main_table set a= (case ..some code.. end), 
                          b= (case ..some code.. end), 
                          c= (select x from sec_table where conditon_1),      
                          d= (select y from sec_table where conditon_1),
                          e= (select z from sec_table where conditon_1) 
    where condition_2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 2013-05-11
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多