使用NOT EXIST优化查询

时间:2013-09-24 08:53:44

标签: sql oracle10g

我想在下面的查询中使用 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)

2 个答案:

答案 0 :(得分: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)索引。

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

此致

答案 1 :(得分:0)

“NOT IN”和“NOT EXISTS”之间存在差异。请点击ASKTOM链接 -

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