Skip to Main Content
  • Questions
  • Performance of recursive view conditioning on CONNECT_BY_ROOT

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Tymur.

Asked: September 27, 2016 - 2:33 pm UTC

Last updated: September 29, 2016 - 3:50 am UTC

Version: Oracle Database 12c Release 12.1.0.1.0 - 64bit Production

Viewed 1000+ times

You Asked

I have some hierarchical data I'd like to look at as if it were a tree. The core of the query looks like following.
SELECT CONNECT_BY_ROOT output_unit_id AS START_UNIT_ID,
       units.*,
       FROM units
       CONNECT BY PRIOR input_unit_id = output_unit_id 


If I make it into a view, I can then do stuff like following.

select * from vw_unit_chain
where START_UNIT_ID = 123456


(It gives me all the units up the transformation chain which were used for given one)

But if I instead use the "start with"-statement, like
SELECT CONNECT_BY_ROOT output_unit_id AS START_UNIT_ID,
       input_units.*,
       FROM input_units
       CONNECT BY PRIOR input_unit_id = output_unit_id
       start with output_unit_id = 123456

the query takes only half as much time.

Is there anything I can do to get both the performance and the query being a view?

and Connor said...

There isnt a way I know of implicitly. Depending on the level the control you have in your application, you could be perhaps use a context do it. For example, your view could be:

SELECT CONNECT_BY_ROOT output_unit_id AS START_UNIT_ID,
units.*,
FROM units
CONNECT BY PRIOR input_unit_id = output_unit_id 
start with output_unit_id = sys_context('MY_CTX','UNIT_ID')


and your application sets the context variable before accessing the view.

Hope this helps.

Rating

  (1 rating)

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

Comments

Tymur Gubayev, September 29, 2016 - 6:37 am UTC

Well, this is unfortunate. I really hoped for something similar to the PUSH_PRED optimizer hint.

I actually have full control to the SQL-queries in the application so I can just insert the whole view's query and add a parameter to it, but this means redundancy and in general isn't as good solution as a view is.