UPDATE blue_inventory AS A LEFT JOIN ( SELECT invent_id, SUM(quantity) AS total,addtime FROM blue_inventory_log AS B WHERE addtime <= UNIX_TIMESTAMP(NOW()) AND addtime > UNIX_TIMESTAMP(NOW()) - 86400 *(select days from blue_inventory AS C where C.id = B.invent_id) GROUP BY invent_id ) AS T ON A.id = T.invent_id SET A.volume = T.total/A.days WHERE A.days > 0
高手给看看这条语句怎么优化一下,总感觉有点怪。不应该查询这么多次
解决方案
15
以文本方式贴出
explain select …
show index from ..
以供分析。
explain select …
show index from ..
以供分析。
25
UPDATE blue_inventory AS A
LEFT JOIN (
SELECT invent_id, SUM(quantity) AS total,addtime
FROM blue_inventory_log AS B,blue_inventory AS C
WHERE addtime <= UNIX_TIMESTAMP(NOW())
AND C.id = B.invent_id
AND addtime > UNIX_TIMESTAMP(NOW()) – 86400 *C.days
GROUP BY invent_id
)
AS T ON A.id = T.invent_id
SET A.volume = T.total/A.days
WHERE A.days > 0
LEFT JOIN (
SELECT invent_id, SUM(quantity) AS total,addtime
FROM blue_inventory_log AS B,blue_inventory AS C
WHERE addtime <= UNIX_TIMESTAMP(NOW())
AND C.id = B.invent_id
AND addtime > UNIX_TIMESTAMP(NOW()) – 86400 *C.days
GROUP BY invent_id
)
AS T ON A.id = T.invent_id
SET A.volume = T.total/A.days
WHERE A.days > 0