处理不肯定深度的层级结构,好比组织机构,一个经常使用的设计是在一张表里面保存 ID 和 Parent_ID ,而且经过自联结的办法构造一颗树。这种方式对写数据的过程很友好,可是查询过程就变得相对复杂。在不引入MPTT模型的前提下,必须经过递归算法来查询某个节点和下级子节点。html
Oracle提供的connect by扩展语法,简单好用。可是其余的RDBMS就没这么人性化了(或者我不知道)。最近在项目中使用PostgreSQL来查询树形数据,记录一下。算法
drop table if exists demo.tree_data;
create table demo.tree_data (
id integer,
code text,
pid integer,
sort integer
);
insert into demo.tree_data values(1, '中国', null, 1);
insert into demo.tree_data values(2, '四川', 1, 1);
insert into demo.tree_data values(3, '云南', 1, 2);
insert into demo.tree_data values(4, '成都', 2, 1);
insert into demo.tree_data values(5, '绵阳', 2, 2);
insert into demo.tree_data values(6, '武侯区', 4, 1);
insert into demo.tree_data values(7, '昆明', 3, 1);
复制代码
若是安装了 tablefunc 扩展,就可使用PG版本的connectby函数。这个没有Oracle那么强大,可是能够知足基本要求。sql
-- API 以下
connectby(text relname, -- 表名称
text keyid_fld, -- id字段
text parent_keyid_fld -- 父id字段
[, text orderby_fld ], -- 排序字段
text start_with, -- 起始行的id值
int max_depth -- 树深度,0表示无限
[, text branch_delim ]) -- 路径分隔符
复制代码
-- 基本用法以下,必须经过AS子句定义返回的字段名称和类型
select *
from connectby('demo.tree_data', 'id', 'pid', 'sort', '1', 0, '~')
as (id int, pid int, lvl int, branch text, sort int);
-- 查询结果
id | pid | lvl | branch | sort
----+-----+-----+---------+------
1 | | 0 | 1 | 1
2 | 1 | 1 | 1~2 | 2
4 | 2 | 2 | 1~2~4 | 3
6 | 4 | 3 | 1~2~4~6 | 4
5 | 2 | 2 | 1~2~5 | 5
3 | 1 | 1 | 1~3 | 6
7 | 3 | 2 | 1~3~7 | 7
(7 rows)
复制代码
-- 仅仅使用基本用法,只能查询出id的相关信息,若是要查询code等其余字段,就须要经过额外的join操做来实现。
select
t.id, n.code, t.pid, p.code as pcode, lvl, branch
from (
select * from connectby('demo.tree_data', 'id', 'pid', 'sort', '1', 0, '~')
as (id int, pid int, lvl int, branch text, sort int)
) as t
left join demo.tree_data as n on (t.id = n.id)
left join demo.tree_data as p on (t.pid = p.id)
order by t.sort ;
id | code | pid | pcode | lvl | branch
----+--------+-----+-------+-----+---------
1 | 中国 | | | 0 | 1
2 | 四川 | 1 | 中国 | 1 | 1~2
4 | 成都 | 2 | 四川 | 2 | 1~2~4
6 | 武侯区 | 4 | 成都 | 3 | 1~2~4~6
5 | 绵阳 | 2 | 四川 | 2 | 1~2~5
3 | 云南 | 1 | 中国 | 1 | 1~3
7 | 昆明 | 3 | 云南 | 2 | 1~3~7
(7 rows)
复制代码
PS:虽然经过join能够查询出节点的code,可是branch部分不能直接转换成对应的code,使用上仍是不太方便。函数
使用CTE语法,经过 with recursive 来实现树形数据的递归查询。这个方法虽然没有connectby那么直接,可是灵活性和显示效果更好。post
--
with recursive cte as
(
-- 先查询root节点
select
id, code, pid, '' as pcode,
code as branch
from demo.tree_data where id = 1
union all
-- 经过cte递归查询root节点的直接子节点
select
origin.id, origin.code, cte.id as pid, cte.code as pcode,
cte.branch || '~' || origin.code
from cte
join demo.tree_data as origin on origin.pid = cte.id
)
select
id,code, pid, pcode, branch,
-- 经过计算分隔符的个数,模拟计算出树形的深度
(length(branch)-length(replace(branch, '~', ''))) as lvl
from cte;
--
id | code | pid | pcode | branch | lvl
----+--------+-----+-------+-----------------------+-----
1 | 中国 | | | 中国 | 0
2 | 四川 | 1 | 中国 | 中国~四川 | 1
3 | 云南 | 1 | 中国 | 中国~云南 | 1
4 | 成都 | 2 | 四川 | 中国~四川~成都 | 2
5 | 绵阳 | 2 | 四川 | 中国~四川~绵阳 | 2
7 | 昆明 | 3 | 云南 | 中国~云南~昆明 | 2
6 | 武侯区 | 4 | 成都 | 中国~四川~成都~武侯区 | 3
(7 rows)
复制代码
执行过程说明ui
从上面的例子能够看出,WITH RECURSIVE语句包含了两个部分spa
执行步骤以下设计
以上面的query为例,来看看具体过程postgresql
-- step 1 执行
select
id, code, pid, '' as pcode,
code as branch
from demo.tree_data where id = 1
-- 结果集和working table为
id | code | pid | pcode | branch
----+------+-----+-------+--------
1 | 中国 | | | 中国
复制代码
-- step 2 执行递归,此时自引用cte中的数据是step 1的结果
select
origin.id, origin.code, cte.id as pid, cte.code as pcode,
cte.branch || '~' || origin.code
from cte
join demo.tree_data as origin on origin.pid = cte.id
-- 结果集和working table为
id | code | pid | pcode | branch
----+--------+-----+-------+---------------------
2 | 四川 | 1 | 中国 | 中国~四川
3 | 云南 | 1 | 中国 | 中国~云南
复制代码
继续执行recursive query,直到结果集和working table为空code
结束递归,将前三个步骤的结果集合并,即获得最终的WITH RECURSIVE的结果集。
严格来说,这个过程实现上是一个迭代的过程而非递归,不过RECURSIVE这个关键词是SQL标准委员会定立的,因此PostgreSQL也延用了RECURSIVE这一关键词。