Skip to Main Content
  • Questions
  • SQL query performance with inner query and join

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Shahenaz.

Asked: September 28, 2019 - 11:39 am UTC

Last updated: October 01, 2019 - 1:18 pm UTC

Version: 11

Viewed 1000+ times

You Asked

I’m querying data for my api to support pagination like below:

Select * from ext_gen_data where order by submitted_dt_tm where ext_data_id>125  and ROWNUM <=5 ;


But rownum does return as expected due to its known behaviour. To solve this problem and as per recommendation, I have modified query as below:

Select * from (Select * from ext_gen_data where order by submitted_dt_tm where ext_data_id>125) where  ROWNUM <=5 ;


For more information, my inner query has column of Blob data type.

My question is what will be performance difference with and without outer query?

Can we have join with inner query, will it impact performance of a query?

Will the inner query fetch all the data which qualifies the criteria and apply filter, what exactly happens on back screen?


Any kind of guideline would be much appreciated.


Thanks,
Shahenaz


and Chris said...

what will be performance difference with and without outer query?

It's kind of an irrelevant question. The first approach gives the wrong answer, so there's no point comparing performance to this.

But in general if you write:

select * from (
  select * from ...
) where ...


If you can rewrite the query (and get the same answer) as

select * from ... where ...


The database will.

When you have:

select * from (
  select t.*, rownum from ...
  order  by ...
) where ...


The database has to run the inner query first to assign row numbers according to the order by. So in this case it has to do the inner filtering and sorting before the outer.

But in general subqueries don't mean "do this first".

See also:

https://asktom.oracle.com/pls/apex/asktom.search?tag=sub-queries-and-joins-performance
https://asktom.oracle.com/pls/apex/asktom.search?tag=subquery-performance

Can we have join with inner query, will it impact performance of a query?

Yes, you can join inside the subquery. Or outside it. And yes it will impact performance. How exactly depends on what you're doing. It might make it faster. It might make it slower.

But I refer to my previous statement. If you need to join to get the correct answer, then comparing to not joining is irrelevant.

Will the inner query fetch all the data which qualifies the criteria and apply filter, what exactly happens on back screen?

What exactly happens on screen depends on how your application handles the data!

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Shahenaz Sangoli, October 01, 2019 - 10:02 am UTC

We have some queries which sort on primary key column and return correct rownum without outer query.

Is it good practice to have all the query with outer query?

How Oracle handle result of inner query, will it cache somewhere?

Do we need to limit the result return by inner query to have good over all result?

Chris Saxon
October 01, 2019 - 1:18 pm UTC

We have some queries which sort on primary key column and return correct rownum without outer query.

This is just chance. You've gotten lucky. Eventually you'll get rows in the "wrong" order.

Is it good practice to have all the query with outer query?

It's good practice to write queries that are guaranteed to give the correct answer!

In some cases this requires a subquery. In some cases it doesn't. Before the fetch first clause in 12c, to write a top-N query you had to use a subquery.

For everything else... it depends on what you're trying to do!

How Oracle handle result of inner query, will it cache somewhere?

Again, it depends.

Do we need to limit the result return by inner query to have good over all result?

What does "good over all result" mean?

Why exactly are you asking these questions? What precisely are you trying to do? BE SPECIFIC. Include example tables + data (create table + inserts) and real queries.

More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database