Vastbase G100 中 connect by 的使用探索
在日常开发场景中,我们经常性会用到递归查询,递归查询用的是 start with
connt by 的方式,这个语式标识的 start with 表示开始查找的节点,connet
by prior 表示关联条件。
语法格式
SELECT [/*+ plan_hint */] [ ALL | DISTINCT | UNIQUE [ ON ( expression
[, ...] ) ] ]
{ * | {expression [ [ AS ] output_name ]} [, ...] }
[ FROM from_item [, ...] ]
[START WITH condition CONNECT BY PRIOR condition]
[ WHERE condition ]
[ [ START WITH condition ] CONNECT BY condition [ ORDER SIBLINGS
BY expression ] ]
大致写法:select * from some_table [where 条件
1] connect by [条件 2] start with [条件 3];
其中 connect by 与 start with 语句摆放的先后顺序不影响查询的结
果,[where 条件 1]可以不需要。
[where 条件 1]、[条件 2]、[条件 3]各自作用的范围都不相同:
[where 条件 1]是在根据“connect by [条件 2] start with [条件
3]”选择出来的记录中进行过滤,是针对单条记录的过滤, 不会考虑树的结
构;
[条件 2]指定构造树的条件,以及对树分支的过滤条件,在这里执行的过滤会
把符合条件的记录及其下的所有子节点都过滤掉;
[条件 3]限定作为搜索起始点的条件,如果是自上而下的搜索则是限定作为根
节点的条件,如果是自下而上的搜索则是限定作为叶子节点的条件;
对 prior 的说明:
prior 存在于[条件 2]中,可以不要,不要的时候只能查找到符合
“start with [条件 3]”的记录,不会在寻找这些记录的子节点。要的时候
有两种写法:connect by prior t.p_id=t.id 或
connect by t.p_id=prior t.id,前一种写法表示采用自上而下的搜索方