[英]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.