0

我想在以下查询中使用NOT EXIST优化我的查询我该怎么做,还请解释它的执行计划

 Select I_Ftn, I_Col, count(c.i_id_num) cnt 
From DSCL_ALL.W_CALENDER c 
Where c.UNIT_CODE= '01' 
AND c.i_g_vill = '45'
and c.i_g_code = '1'
and c.survey_year = '2012-2013'
and c.i_number not in (select m.m_indent from w_mill_pur m where m.unit_code = c.unit_code and m.m_vill = c.i_g_vill and m.m_grow = c.i_g_code)  
Group By I_Ftn, I_Col
ORDER BY I_ftn, I_col)
4

2 回答 2

1

你可能想试试这个:

Select I_Ftn, I_Col, count(c.i_id_num) cnt 
From DSCL_ALL.W_CALENDER c 
Where c.UNIT_CODE= '01' 
AND c.i_g_vill = '45'
and c.i_g_code = '1'
and c.survey_year = '2012-2013'
and not exists (select 1 from w_mill_pur m where m.unit_code = c.unit_code and m.m_vill = c.i_g_vill and m.m_grow = c.i_g_code and m.m_indent = c.i_number)  
Group By I_Ftn, I_Col
ORDER BY I_ftn, I_col)

由于添加了 where 子句,它更有效:Oracle 能够运行更过滤的子查询,然后只测试结果集是否为空。您可能还想检查是否有 w_mill_pur 的 (unit_code, m_vill, m_grow, m.m_indent) 索引。

“不在”方式需要在主查询中再加入一次(子查询结果集与主查询)。

问候,

于 2013-09-24T09:00:37.030 回答
0

“不在”和“不存在”之间是有区别的。请点击 ASKTOM 的链接 -

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::NO::P11_QUESTION_ID:442029737684

于 2013-09-24T09:53:23.037 回答