I have query like this
select unique so.object_id, bpi_id
from order_items,
nc_objects so
where so.object_type_id = 9062352550013045460 /* Sales Order */
start with so.object_id = order_items.object_id
connect by prior so.parent_id = so.object_id
Oracle version: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
'explain plan' shows that there is no join conditions between tables so and order_items
Can someone explain me why they are not connected by the start with statement?
I rewrited my initial query:
select unique (select so.object_id
from nc_objects so
where so.object_type_id = 9062352550013045460 /* Sales Order */
start with so.object_id = order_items.object_id
connect by prior so.parent_id = so.object_id) so_id,
bpi_id
from order_items
It works, but only because of so have one row per order_items row.
If you do:
Then for the sample data:
The output is:
You can see from the path that the
so.object_idonly equals theorder_items.object_idfor theSTART WITHrow but for all the descendant rows everyso.object_idis connected to everyorder_items.object_idwith aCROSS JOIN.If you want to rewrite it as a recursive query then:
Which outputs the same.
You can see that the join condition
WHERE so_id = oi_idis only applied on the first join of the recursive query, the equivalent of theSTART WITHclause of the hierarchical query, and is not applied to any of the rows generated in the recursive part of the query, the equivalent of theCONNECT BYclause; therefore, it is not appropriate to join the two tables using theSTART BYclause as that only applies to the first row of the hierarchy and the rest areCROSS JOINed.If you did want to
JOINat each level of the hierarchy then don't use aCROSS JOINand useINNER JOINinstead:Which outputs:
fiddle