递归查询性能不佳

时间:2021-03-11 11:03:46

标签: java oracle performance recursion

我们有一个包含文档和文件夹的系统,每个文档和文件夹对每个用户都有不同的访问权限 数据库已经包含了数百万条数据,最终访问文档应该做递归,以确保用户可以访问所有父文件夹 搜索性能很差,我们尝试了很多想法,但仍然面临性能问题 enter image description here

1 个答案:

答案 0 :(得分:0)

您应该像这样使用 with clause。 (sqlite3 示例)

create table tree (id int, parent_id int);
insert into tree (id, parent_id) values
(1, null), (2, 1), (3, 1), (4, 1), (5, 2), (6, 2), (7, 3), (8, 7);
with r as (
    select * from tree where id = 8
    union all
    select tree.* from tree, r where tree.id = r.parent_id
) select * from r;

输出(8 个的所有父母):

8|7
7|3
3|1
1|

树结构:

enter image description here