1

下面的查询似乎需要大约 20 秒才能执行,并且因为它在单个事务中运行多次,所以严重影响了性能。

[update table1
            set column3 = 'new_str'  
          where column1||','||column2 in  
        (select table1.column1||','||column2  
           from table1   
       join table2 on table1.column1 = table2.column1  
      where table2.column4 = 'value4'  
        and table1.column2 = 'value2'  
        and column3 = 'old_str')]  

表 1
column1 - char (12) - 主键
column2 - char (30) - 主键
column3 - char (25)

table2
column1 - char (12) - 主键(表 1 中的外键)
column4 - char (12)

上面的表大约有 1009578 和 1082555 条记录。

4

3 回答 3

0

无法测试它,但我认为打破基于计算字段的标准应该会大大加快更新速度。像这样的东西(可能缺少一些东西)应该会更好:

[update table1
         set column3 = 'new_str'  
          where column1 in   
        (select table1.column1  
           from table1   
      where table1.column2 = 'value2'  
        and column3 = 'old_str')
        and 
         column2 in 
    (select table2.column2  
           from table2   
      where table2.column1 = column1
        and table2.column4 = 'value4')
        ] 
于 2013-03-04T14:56:53.853 回答
0

我认为您正在对 Table1 进行不必要的查询。尝试这个:

 update table1 t1
 set column3 = 'new_str'  
 where EXISTS 
  (select *          
   from table2 t2
   where 
     t1.column1 = t2.column1 -- this is your link from t1 to t2
     and t2.column4 = 'value4'  
     and t1.column2 = 'value2'  
     and t2.column3 = 'old_str'
   )
于 2013-03-04T15:26:41.657 回答
0

我想这个IN原因在这里不是必需的:

update table1
    set column3 = 'new_str' 
    from table1 join table2 on table1.column1 = table2.column1  
      where table2.column4 = 'value4'  
        and table1.column2 = 'value2'  
        and table1.column3 = 'old_str'

写给我们解决方案是最快的;]!

于 2013-03-04T15:30:42.743 回答