【发布时间】:2016-05-31 05:45:38
【问题描述】:
表定义:
create table Tree
(node varchar2(20),
parentNode varchar2(20),
val number);
create index idx_tree_01 on Tree
(node );
create index idx_tree_02 on Tree
(parentnode);
样本数据:
Insert into TREE (NODE,PARENTNODE,VAL) values ('2','1',2);
Insert into TREE (NODE,PARENTNODE,VAL) values ('3','2',3);
Insert into TREE (NODE,PARENTNODE,VAL) values ('4','2',3);
Insert into TREE (NODE,PARENTNODE,VAL) values ('5','4',1);
Insert into TREE (NODE,PARENTNODE,VAL) values ('6','3',1);
查看定义:
create view tree_view as
select connect_by_root parentnode as firstNode,
lpad(' ', 2 * level - 2, ' ') || val as MyVal,
node, parentNode
from tree
start with parentnode in ( select parentnode from tree)
connect by parentnode = prior node
现在我要执行查询:
select * from tree_view
where firstNode = '1'
查询的执行计划是:
-------------------------------------------------------------------------
| Id | Operation | Name | E-Rows |
-------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | |
|* 1 | VIEW | TREE_VIEW | 5 |
|* 2 | CONNECT BY NO FILTERING WITH START-WITH| | |
| 3 | TABLE ACCESS FULL | TREE | 5 |
|* 4 | INDEX RANGE SCAN | IDX_TREE_02 | 1 |
-------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("FIRSTNODE"='1')
2 - access("PARENTNODE"=PRIOR NULL)
filter( IS NOT NULL)
4 - access("PARENTNODE"=:B1)
如您所见,最后使用了filterfirstnode = '1'。我希望它被用作第一件事..
我不能修改视图(我只能添加一些提示)。
如何提示 Oracle 在 START WITH 中使用 firstNode 值?它将带来很大的性能改进。
我真正的“树”表是几个表,数据量很大。 正如我提到的,我无法修改视图。
不要向我求婚:
- 带有 firstnode 参数和流水线结果的函数
- 修改视图以在开头使用会话上下文
- 以 开头的临时表队列
- 等。
视图必须相同。仅允许使用提示。
【问题讨论】:
-
@PavelGatnar 我不会在这里问什么时候会有这种可能性。该项目已上线 - 不允许进行较大的修改。
-
只是示例。在现实中,有来自其他表的结果。请注意我的问题是如何将过滤从查询之外移到 START WITH。
-
你解决了吗?你能说出你尝试了哪些提示吗?
标签: sql oracle oracle11g database-performance connect-by