Hi Arnab,
>> Query 2 might contain any subquery as the filter condition, I have selected the above as an example only
Yes, but it will essentially return one fow for every row in table_2 . . .
This does not make sense!
I think you are confusing ROWNUM with ROWID:
http://www.dba-oracle.com/t_rownum_rowid_difference.htm*************************************************
Using rownum is very dangerous, especially in the hands of beginners, and there are always alternatives to using rownum:
- The WITH clause - Another approach to tuning rownum queries is to separate-out the subquery using the powerful WITH clause. Another benefit of separating-out the sorted subquery is that you can easily apply either a parallel hint or an index hint, if it's faster to retrieve the rows in pre-sorted order.
- The rank or row_number analytics - You can replace rownum in top-n queries with analytics functions, using rank() or row_number() instead, getting the same top-in result, but with much faster response time.
- Optimizer goal hint - In cases where rownum is used to change the optimizer mode to first_rows_n, it may be possible to negate this effect by using an all_rows hint.
- Index hint - In cases where rownum is used to force an index in a subquery, again, deploy an index hint to duplicate the faster execution plan.
The ROWNUM filters RANDOM rows unless it is inside a subquery:
>> Select * from table_1 where rownum in (1,2,3,4,5)
This should be select * from table_1 where rownum < 6;
But it will return six random rows.Read this carefully, it explains all:
http://www.dba-oracle.com/t_sql_tuning_rownum_equals_one.htm