0

I have three database tables which contains large amount of data(6000). I wrote the following query for some operaton which is working fine.

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 );

But the performance of this query is bit slow. it is taking 21 seconds to update the records. Where can i improve this query. Any suggestions will be greatly appriciated

4

1 回答 1

0

不了解底层数据模型会使此查询难以理解。比如我看不懂update语句的where子句:

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

这是否意味着您只想更新 ID 等于 productmasterID 的记录?group by 子句的目的是什么?

为了更好的可读性:

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);
于 2012-11-05T12:25:50.913 回答