DB2,优化更新查询

时间:2015-10-27 07:38:41

标签: sql db2

我有这个问题:

update product a 
  set a.tsinsert = (select b.tsinsert 
                      from h_product b 
                     where b.tsinsert is not null and 
                           a.product_id = b.product_id);

此查询未结束。 我可以用它来写吗?

2 个答案:

答案 0 :(得分:0)

我使用了这个查询并且有效:

merge into  product a using (select distinct product_id ,TSINSERT  from  h_product where tsinsert is not null order by product_id ) b
                on (a.product_id = b.product_id ) 
                when matched then update set a.tsinsert = b.TSINSERT;

答案 1 :(得分:0)

对于此查询:

update product a 
  set a.tsinsert = (select b.tsinsert 
                      from h_product b 
                     where b.tsinsert is not null and 
                           a.product_id = b.product_id);

您需要h_product(product_id, tsinsert)上的索引。我很惊讶查询没有where子句,因为当没有匹配时,这会将a.tsinsert设置为NULL