簡體   English   中英

遞歸查詢性能不佳

[英]bad performance for recursion query

我們有一個包含文檔和文件夾的系統,每個用戶都有不同的訪問權限數據庫已經包含數百萬條數據,文檔的最終訪問應該做遞歸以確保用戶可以訪問所有父文件夾搜索性能非常好很差,我們嘗試了很多想法,但仍然面臨性能問題在此處輸入圖像描述

你應該像這樣使用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;

output(所有父母為8):

8|7
7|3
3|1
1|

樹形結構:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM