码迷,mamicode.com
首页 > 数据库 > 详细

sql语句优化之not in

时间:2015-12-30 17:02:29      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

多表关联想查a表中除去b表的可用not exists 效率比not in 更高

优化后的语句用时0.421秒

select john.*,
(case
when round((case john.sumcount
when 0 then
0
else
mycount * 100 / sumcount
end),
2) > 100 then
100
else
round((case john.sumcount
when 0 then
0
else
mycount * 100 / sumcount
end),
2)
end) lv
from (select ‘路径外‘ kind,
cp_id,
(select cp_node_name
from lcp_master_node n
where n.cp_id = o.cp_id
and n.cp_node_id = o.cp_node_id) node_name,
cp_node_id,
LOCAL_ORDER_TEXT,
LOCAL_ORDER_NO,
MEASURE,
IS_ANTIBIOTIC,
MEASURE_UNITS,
count(*) mycount,
(select count(*)
from (select count(*)
  from LCP_PATIENT_LOG_ORDER r
 where r.cp_id = ‘10896‘
 group by patient_no)) sumcount
from LCP_PATIENT_LOG_ORDER o
where cp_id = ‘10896‘
group by cp_id,
cp_node_id,
LOCAL_ORDER_TEXT,
LOCAL_ORDER_NO,
MEASURE,
IS_ANTIBIOTIC,
MEASURE_UNITS) john
where NOT EXISTS
(select *
from lcp_node_order_item i
where i.cp_id = john.cp_id
and i.cp_node_id = john.cp_node_id
and i.CP_NODE_ORDER_TEXT = john.local_order_text)
order by cp_node_id, mycount desc;

  

sql语句优化之not in

标签:

原文地址:http://www.cnblogs.com/wuhailong/p/5089199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!