In clause versus OR clause performance wise

橙三吉。 提交于 2019-12-23 04:52:10

问题


I have a query as below:

select * 
from table_1 
where column_name in ('value1','value2','value3');

considering that the data in such a table may be in millions, will the below restructuring help better??

select * 
from table_1 where 
column_name = 'value1' 
or column_name = 'value2' 
or column_name ='value3';

or

select * 
from table_1 
where column_name = any ('value1','value2','value3');

I need to know performance benefits also if possible.

Thanks in advance


回答1:


the query doesn't matter much in case of 3 value checking only.

Oracle will re-write the query anyways to match the best option available.

in case there were more values and that too dynamic then the in clause or inner join could have been better.

its best to leave the query as it is currently




回答2:


There is a 3rd way which is faster than 'IN' or multiple 'WHERE' conditions:

select *
from table_1 as tb1
inner join table_2 as tb2
where tb1.column_name = tb2.column_name

Here table_2 (or query) would have required values that were listed in 'IN' and 'WHERE' conditions in your example.



来源:https://stackoverflow.com/questions/15334695/in-clause-versus-or-clause-performance-wise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!