首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以为下面的SQL查询删除UNION

是否可以为下面的SQL查询删除UNION
EN

Stack Overflow用户
提问于 2014-04-16 04:41:29
回答 1查看 53关注 0票数 0

我想优化下面的查询。一小部分记录需要2分钟以上的时间。是否可以删除UNION并优化查询

代码语言:javascript
复制
Select cia.cia_orderamount, cia.cia_notes, cia.cia_mincount, cia.cia_customerid, cia.cia_id, cia.cia_locationid, lc.locationcode, T1.instock, c.id as customerid, c.customer
From cardinventoryalerts cia
Inner join customers c on cia.cia_customerid = c.id and c.useautocardorder = 1
Inner Join locationcodes lc on cia.cia_locationid = lc.id
left JOIN 
  (SELECT cb.customer, Case when cb.locationcode is null or cb.locationcode = '' Then NULL Else cb.locationcode End as locationcode,
    sum( CASE WHEN cb.issued = 'no' THEN 1 ELSE 0 END) as instock
  FROM cardbatch cb
  Group By cb.customer, cb.locationcode
  ) as T1 on lc.locationcode = T1.locationcode and T1.customer = c.customer
WHERE (cia_mincount > T1.instock or T1.instock is NULL)

UNION
Select cia.cia_orderamount, cia.cia_notes, cia.cia_mincount, cia.cia_customerid, cia.cia_id, -1, NULL, T1.instock, c.id as customerid, c.customer
From cardinventoryalerts cia
Inner join customers c on cia.cia_customerid = c.id and c.useautocardorder = 1
left JOIN 
  (SELECT cb.customer, -1, sum( CASE WHEN cb.issued = 'no' THEN 1 ELSE 0 END) as instock
  FROM cardbatch cb
  where cb.locationcode is null or cb.locationcode = ''
  Group By cb.customer
  ) as T1 on c.customer = T1.customer
WHERE (cia_mincount > T1.instock or T1.instock is NULL) and cia.cia_locationid is null

对此查询的解释:

代码语言:javascript
复制
    UNION RESULT    <union1,3>  ALL                     
1   PRIMARY c   ALL PRIMARY,id              2   Using where
1   PRIMARY cia ALL fk_ciacustid,fk_cialocationid               3   Using where
1   PRIMARY lc  eq_ref  PRIMARY,id  PRIMARY ... quiz.cia.cia_locationid 1   
1   PRIMARY <derived2>  ALL                 3   Using where
2   DERIVED cb  index       customer_loc    ...     3   
3   UNION   cia ref fk_ciacustid,fk_cialocationid   fk_cialocationid    ... const   1   Using where
3   UNION   c   eq_ref  PRIMARY,id  PRIMARY ... quiz.cia.cia_customerid 1   Using where
3   UNION   <derived4>  ALL                 1   Using where
4   DERIVED cb  index       customer_loc    ...     3   Using where
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-16 05:41:49

我认为您可以将这两个部分结合在一起(参见下面),将内部连接位置代码替换为左联接位置代码,并在条件适当的情况下编写

代码语言:javascript
复制
From cardinventoryalerts cia
Inner join customers c on cia.cia_customerid = c.id and c.useautocardorder = 1
left Join locationcodes lc on cia.cia_locationid = lc.id
left JOIN 
  (SELECT cb.customer, Case when cb.locationcode is null or cb.locationcode = '' Then NULL Else cb.locationcode End as locationcode,
    sum( CASE WHEN cb.issued = 'no' THEN 1 ELSE 0 END) as instock
  FROM cardbatch cb
  Group By cb.customer, cb.locationcode
  ) as T1 on lc.locationcode = T1.locationcode and T1.customer = c.customer
left JOIN 
  (SELECT cb.customer, -1, sum( CASE WHEN cb.issued = 'no' THEN 1 ELSE 0 END) as instock
  FROM cardbatch cb
  where cb.locationcode is null or cb.locationcode = ''
  Group By cb.customer
  ) as T1 on c.customer = T1.customer
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23100129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档