我有以下问题。在oracle12 db中有数百万个元素的hirarchy。如何选择前N个层次结构对象。应该选择哪个对象并不重要,唯一的要求是对象应该处于层次结构中。 Rownum过滤器根本不起作用:
SELECT object_id
FROM objects
WHERE rownum < 10
START WITH object_id = 9072243521013844211
CONNECT BY PRIOR object_id = parent_id
我得到以下执行计划:在对象选择后进行过滤。
/*
LEVEL PLAN ROWS COST PREDICATES
*/
1 SELECT STATEMENT (OPTIMIZER=ALL_ROWS) 8 1
2 COUNT
3 FILTER filter(ROWNUM<10)
4 CONNECT BY (WITH FILTERING) access("PARENT_ID"=PRIOR "OBJECT_ID")
5 TABLE ACCESS (BY INDEX ROWID) "OBJECTS"
6 INDEX (UNIQUE SCAN) "XPKOBJECTS" 1 1 access("OBJECT_ID"=9072243521013844211)
5 NESTED LOOPS
6 CONNECT BY PUMP
6 TABLE ACCESS (BY INDEX ROWID BATCHED) "OBJECTS" 8 1
7 INDEX (RANGE SCAN) "XIF25OBJECTS" 8 1 access("PARENT_ID"=PRIOR "OBJECT_ID")
答案 0 :(得分:0)
以下示例解决方案应提供一些指导:
with objects as
(select level object_id, decode(level+1, :len+1, null, level+1) parent_id
from dual connect by level<=:len )
SELECT object_id
FROM objects
where level <= :n_objects
START WITH object_id = 1
CONNECT BY PRIOR parent_id = object_id
;