我有三个包含大量数据的数据库表(6000)。我为一些工作正常编写了以下查询。
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秒。我在哪里可以改进此查询。任何建议都会受到很大关注
答案 0 :(得分: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);