Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Joe.

Asked: June 04, 2008 - 11:31 am UTC

Last updated: February 01, 2011 - 4:37 pm UTC

Version: 10-11g

Viewed 10K+ times! This question is

You Asked

When using WITH query sub-factoring, the docs. state that this may be performed behind the scenes by either creating a temp. table or an in-line view.

When choosing the temp. table option, what happens behind the scenes about indexing, and can we influence it?


For example,
WITH ABC AS
  (SELECT COLUMN_1
   FROM   TABLE_ABC),
   --
SELECT *
FROM   XYZ, ABC
WHERE  XYZ.COLUMN_1 = ABC.COLUMN_1

Can ABC.COLUMN_1 be 'indexed' ?

and Tom said...

there will be no indexing, the concept here is you are doing something "in bulk"


In your above query, Oracle would tend NEVER to use indexes - period, it would not make sense for the default optimization mode (all rows) and the fact that every row in XYZ and ABC will be processed. that would almost certainly be two full scans and a hash join.

However, if you tweaked the query to something like:
WITH ABC AS
  (SELECT COLUMN_1
   FROM   TABLE_ABC),
   --
SELECT *
FROM   XYZ, ABC
WHERE  XYZ.COLUMN_1 = ABC.COLUMN_1
 and abc.column_1 = 1





in this case - in the above, if column_1 were indexed and selective - we would NOT generate the factored subquery, we would merge the predicate and use the index.

that is, the optimizer does the right thing, materializing when it makes sense, not doing so when it does not.

Rating

  (1 rating)

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

Comments

Difference between with and In

snehasish Das, January 31, 2011 - 4:04 am UTC

Hi Tom,

Can you clarify the way oracle differntiates the IN and WITH clause for a similiar type of query.

Say

Select * from TABLE1 where COLUMN1 in (select COLUMN1 from table2,table3 where table2.column2 = table3.column3 );

and same with the WITH clause of the subquery.

Thanks and Regards,
Snehasish Das.
Tom Kyte
February 01, 2011 - 4:37 pm UTC

conceptually they are the same. The with clause is a little bit of a hint to the optimizer to consider materializing the query into temp - more so than an inline view - but conceptually they are the same.

Most of the times you would expect the same plan.


More to Explore

Performance

Get all the information about database performance in the Database Performance guide.