2013-09-24 44 views
0

我想用在下面的查询我怎么可以这样做NOT EXIST优化我的查询,也请您解释一下它的执行计划优化查询使用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) 

回答

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能够运行更多筛选子查询,然后仅测试结果集是否为空。 您可能还需要检查你有一个(unit_code,m_vill,m_grow,m.m_indent)为w_mill_pur指数。

的“不”的方式需要在主查询(子查询结果与主一套)一个更加入。

问候,

+1

甲骨文是非常聪明的。如果您的陈述会导致与原始执行计划相同的执行计划,我不会感到惊讶。 –

+0

我想这取决于可用索引,但是,Oracle优化器在这方面非常出色。 同时,具有更好的原始查询仍然是一个更好的起点:) – FabienM

+0

呜我以前的查询的执行时间是14.53,现在它正在仅为1秒。谢谢FabienM。 – Ravi