2012-11-05 60 views
0

我有三個數據庫表,其中包含大量的數據(6000)。我爲一些正在工作的operaton寫了下面的查詢。如何提高此oracle查詢的性能?

UPDATE productlistprice prl SET prl.value = (
    SELECT minprice FROM(
     SELECT MIN(prl.value) AS minprice, prv.productmasterid AS masterprid 
      FROM product pr , productlistprice prl, productvariation prv 
       WHERE prv.productmasterid = (
     SELECT prv.productmasterid FROM productvariation pv 
       WHERE pv.productid = pr.uuid) 
       AND prl.productid = pr.uuid AND prl.productid = prv.productid 
        GROUP BY prv.productmasterid) 
       WHERE masterprid = (SELECT prv.productmasterid 
       FROM productvariation prv WHERE prv.productmasterid = prl.productid 
        GROUP BY prv.productmasterid)) 
       WHERE prl.productid = (
     SELECT prv.productmasterid FROM productvariation prv 
       WHERE prv.productmasterid = prl.productid 
GROUP BY prv.productmasterid); 

但是這個查詢的性能有點慢。它需要21秒來更新記錄。我在哪裏可以改進此查詢。任何建議將會大大增加

+1

你怎麼能連看了嗎? :)你有什麼指數? – GolezTrol

+1

我會開始通過使用'join'關鍵字重寫您的查詢。 – podiluska

+2

@podiluska確實加入關鍵字提高了性能? – user964147

回答

0

不理解底層數據模型會使該查詢難以理解。例如, 我不明白update語句的where子句:

update productlistprice prl 
    ... 
where prl.productid = (select prv.productmasterid 
          from productvariation prv 
         where prv.productmasterid = prl.productid 
         group by prv.productmasterid); 

這是否意味着你只需要以等於productmasterID的ID來更新記錄?這個小組通過子句的目的是什麼?

爲了更好的可讀性:

update productlistprice prl 
    set prl.value = 
     (select minprice 
      from (select min(prl.value) as minprice 
         ,prv.productmasterid as masterprid 
        from product   pr 
         ,productlistprice prl 
         ,productvariation prv 
       where prv.productmasterid = (select prv.productmasterid 
               from productvariation pv 
               where pv.productid = pr.uuid) 
        and prl.productid = pr.uuid 
        and prl.productid = prv.productid 
       group by prv.productmasterid) 
     where masterprid = (select prv.productmasterid 
           from productvariation prv 
           where prv.productmasterid = prl.productid 
           group by prv.productmasterid)) 
where prl.productid = (select prv.productmasterid 
          from productvariation prv 
         where prv.productmasterid = prl.productid 
         group by prv.productmasterid); 
+0

productmasterid是數據庫表中的外鍵,所以存在多個相同的id,這就是爲什麼我將它們分組 – user964147

+0

是的,我想更新id等於productmasterid的記錄 – user964147

+1

One改進將是使用「存在」。 where exists(select prv.productmasterid from productvariation prv where prv.productmasterid = prl.productid) – Rene