更新表基于并发连接

时间:2018-06-05 13:16:42

标签: sql oracle

我有一个下面的查询运行完美但后来我在表(Export_SKU_Data_v2)被锁定的完整数据上运行它。锁是Row-X锁。 令人惊讶的是,计数只有大约30,000行,我不认为这个问题与性能有关。问题肯定在于查询。

update Export_SKU_Data_v2 tgt
    set tgt.seo_url
    = (select src.seo_url--,src.sku_id,src.site_id
            from (
  WITH prd_sites AS 
   (   
          select wo.Product_id, WO.Site_id
           FROM
             (select decode(substr(translation_id, 1, 1), '2', 'BuyBuyCaby', '3', 'CedCathCanada', null) site_id,
                                product_id
                              from bbb_prod_site_translations
                              where translation_id in ('2_en_US_webOffered_Y', '3_en_US_webOffered_Y')
               )wo,  
              (select decode(substr(translation_id, 1, 1), '2', 'BuyBuyCaby', '3', 'BedCathCanada', null) site_id, 
                                          product_id
                                        from bbb_prod_site_translations
                                        where translation_id in ('2_en_US_prodDisable_N', '3_en_US_prodDisable_N')                                  
                )da                              
              Where wo.product_id = da.product_id and wo.site_id = da.site_id              
              UNION
              Select Product_id, 'BedCathUS' from bbb_product
              where web_offered_flag= 1 and disable_flag = 0
   )select distinct sku_id,site_id,seo_url as seo_url from (                      
                    select k.sku_id,bp.product_id,bp.web_offered_flag,bp.disable_flag,bp.seo_url,site_id,
                    dense_rank() over(partition by k.sku_id,site_id order by bp.product_id desc) as rnk
                    from (
                    select bs.sku_id,count(*) 
                    from bbb_sku bs
                    inner join DCS_PRD_CHLDSKU dpcs on bs.sku_id=dpcs.sku_id
                    group by bs.sku_id
                    having count(*)>1
                    ) k inner join DCS_PRD_CHLDSKU dpcs1 on k.sku_id=dpcs1.sku_id
                    inner join bbb_product bp on dpcs1.product_id=bp.product_id
          inner join prd_sites pst on bp.product_id=pst.product_id
                    where bp.seo_url is not null
                    ) tab where rnk=1
    )src where tgt.sku_id = src.sku_id and tgt.site_id=src.site_id )
    where tgt.seo_url is null ;--and tgt.sku_id='10920540';

注意:请允许我询问这是否是基于上述逻辑更新表的正确方法。逻辑非常简单,我无法找到我搞砸的地方。

1 个答案:

答案 0 :(得分:0)

此更新命令:

   update Export_SKU_Data_v2 tgt
        set tgt.seo_url
    .....
     where tgt.seo_url is null ;

自动获取两种锁:

  • 行共享锁(RS) - 也称为子共享表锁(SS) - at 表级
  • 所有行上的
  • 行锁,也称为TX锁 由命令更新,即满足的所有行 条件:where tgt.seo_url is null

这不是问题 - 这是正常和预期的行为 您可以在文档中找到有关此主题的更多信息:Automatic Locks in DML Operations