SqL查询性能问题

时间:2013-03-04 14:50:29

标签: sql performance

下面的查询似乎需要大约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)

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

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

3 个答案:

答案 0 :(得分: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')
        ] 

答案 1 :(得分: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'
   )

答案 2 :(得分: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'

用解决方案写我们最快;]!