为了账号安全,请及时绑定邮箱和手机立即绑定

mysql UNION ALL 优化问题

mysql UNION ALL 优化问题

留后门 2018-01-30 20:02:53
UPDATE 表名0 a,(SELECT b.storage_id,b.sku_id,SUM(b.num) num,SUM(b.check_num)check_num FROM (SELECT a.storage_id,b.sku_id,SUM(b.num) num,SUM(CASE WHEN a.process_step IN ('3','4','73','74') THEN b.num ELSE 0 END) check_num FROM 表名 1,表名 2 WHERE a.business_id=b.business_id AND a.tid=b.tid AND b.sku_id>0 AND a.business_id= 9023AND a.process_step IN ('0','2','3','4','70','73','74','7') AND a.disable='true' GROUP BY a.storage_id,b.sku_id  UNION ALL  SELECT a.storage_id,c.ch_sku_id,SUM(b.num*c.num) num,SUM(CASE WHEN a.process_step IN ('3','4','73','74') THEN b.num*c.num ELSE 0 END) check_num FROM  表名1 a,表名2 b,表名3 c WHERE a.tid=b.tid AND a.business_id=b.business_id AND b.sku_id=c.sku_id AND b.business_id=c.business_id  AND b.sku_id>0 AND a.business_id=9023 AND a.process_step IN ('0','2','3','4','70','73','74','7') AND a.disable='true' GROUP BY a.storage_id,c.ch_sku_id)b  GROUP BY b.storage_id,b.sku_id) b SET a.pay_count=b.num,a.check_count=b.check_num WHERE a.sku_id=b.sku_id AND a.storage_id = b.storage_id 求大神教我优化
查看完整描述

1 回答

?
Chasing_Cars

TA贡献3条经验 获得超0个赞

字段索引设置了吗

多表关联不要用where,用join+子查询能快很多

查看完整回答
反对 回复 2018-02-07
  • 1 回答
  • 0 关注
  • 2051 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信