给看看这条语句怎么优化一下

MySql 码拜 8年前 (2016-07-12) 864次浏览
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 ..
以供分析。

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

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明给看看这条语句怎么优化一下
喜欢 (0)
[1034331897@qq.com]
分享 (0)