mysql UNION ALL 优化问题

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= 9023

AND 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 

求大神教我优化


留后门
浏览 2052回答 1
1回答

Chasing_Cars

字段索引设置了吗多表关联不要用where,用join+子查询能快很多
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MySQL