關係型資料庫存儲樹形數據(分類、組織架構、地區)常用兩種模型:鄰接表(親子引用)與嵌套集合(左右邊界)。本文深度解析嵌套集合模型的設計、核心查詢(路徑、深度、子樹、直系子節點)、節點增刪改維護,並以「省-市-區」三級聯動為例落地。
| 維度 | 鄰接表 | 嵌套集合 |
|---|---|---|
| 結構 | parent_id 自引用 | lft, rgt 邊界值 |
| 查詢子孫 | 遞歸/CTE 多次掃描 | 單條 BETWEEN 查詢 |
| 查詢祖先 | 遞歸/CTE | 單條 BETWEEN 反向查詢 |
| 插入/移動節點 | 極簡單 | 需更新大量邊界值 |
| 適用場景 | 頻繁變動、深度不固定 | 讀多寫少、固定深度、報表分析 |
CREATE TABLE nested_category ( category_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20) NOT NULL, lft INT NOT NULL, rgt INT NOT NULL ) ENGINE=InnoDB; -- 先序遍歷編號:從根節點 lft=1 開始,每進入節點 +1,離開 +1 INSERT INTO nested_category VALUES (1,'ELECTRONICS',1,20), (2,'TELEVISIONS',2,9), (3,'TUBE',3,4), (4,'LCD',5,6), (5,'PLASMA',7,8), (6,'PORTABLE ELECTRONICS',10,19), (7,'MP3 PLAYERS',11,14), (8,'FLASH',12,13), (9,'CD PLAYERS',15,16), (10,'2 WAY RADIOS',17,18);
核心性質:子節點的 lft/rgt 必嚴格包含於父節點區間 (parent.lft, parent.rgt) 內;葉子節點滿足 rgt = lft + 1。
SELECT
CONCAT(REPEAT(' ', COUNT(parent.name)-1), node.name) AS name,
(COUNT(parent.name)-1) AS depth
FROM nested_category AS node
JOIN nested_category AS parent ON node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft;
SELECT parent.name FROM nested_category AS node JOIN nested_category AS parent ON node.lft BETWEEN parent.lft AND parent.rgt WHERE node.name = 'FLASH' ORDER BY parent.lft;
SELECT name FROM nested_category WHERE rgt = lft + 1;
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth FROM nested_category AS node JOIN nested_category AS parent ON node.lft BETWEEN parent.lft AND parent.rgt JOIN nested_category AS sub_parent ON node.lft BETWEEN sub_parent.lft AND sub_parent.rgt JOIN ( SELECT node.name, (COUNT(parent.name)-1) AS depth FROM nested_category AS node JOIN nested_category AS parent ON node.lft BETWEEN parent.lft AND parent.rgt WHERE node.name = 'PORTABLE ELECTRONICS' GROUP BY node.name ) AS sub_tree ON sub_parent.name = sub_tree.name GROUP BY node.name ORDER BY node.lft;
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth FROM ...(同上) HAVING depth <= 1 ORDER BY node.lft;
LOCK TABLE nested_category WRITE;
SELECT @myRight := rgt FROM nested_category WHERE name = 'TELEVISIONS';
UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myRight;
UPDATE nested_category SET lft = lft + 2 WHERE lft > @myRight;
INSERT INTO nested_category(name, lft, rgt) VALUES('GAME CONSOLES', @myRight+1, @myRight+2);
UNLOCK TABLES;
LOCK TABLE nested_category WRITE;
SELECT @myLeft := lft FROM nested_category WHERE name = '2 WAY RADIOS';
UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myLeft;
UPDATE nested_category SET lft = lft + 2 WHERE lft > @myLeft;
INSERT INTO nested_category(name, lft, rgt) VALUES('FRS', @myLeft+1, @myLeft+2);
UNLOCK TABLES;
LOCK TABLE nested_category WRITE; SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1 FROM nested_category WHERE name = 'GAME CONSOLES'; DELETE FROM nested_category WHERE lft BETWEEN @myLeft AND @myRight; UPDATE nested_category SET rgt = rgt - @myWidth WHERE rgt > @myRight; UPDATE nested_category SET lft = lft - @myWidth WHERE lft > @myRight; UNLOCK TABLES;
LOCK TABLE nested_category WRITE; SELECT @myLeft := lft, @myRight := rgt, @myWidth := rgt - lft + 1 FROM nested_category WHERE name = 'PORTABLE ELECTRONICS'; DELETE FROM nested_category WHERE lft = @myLeft; -- 僅刪除自身行 UPDATE nested_category SET rgt = rgt - 1, lft = lft - 1 WHERE lft BETWEEN @myLeft AND @myRight; -- 子孫整體左移 1 UPDATE nested_category SET rgt = rgt - 2 WHERE rgt > @myRight; UPDATE nested_category SET lft = lft - 2 WHERE lft > @myRight; UNLOCK TABLES;
CREATE TABLE `region` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(30) DEFAULT NULL, `parent_id` INT(11) DEFAULT NULL, -- 保留鄰接引用便於應用層編輯 `lft` INT(10) UNSIGNED DEFAULT NULL, `rgt` INT(10) UNSIGNED DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `region` VALUES (1,'中國',0,1,20), (2,'北京',1,2,5), (3,'北京市',2,3,4), (4,'上海',1,6,9), (5,'上海市',4,7,8), (6,'浙江',1,10,19), (7,'金華市',6,15,16), (8,'溫州市',6,17,18), (9,'杭州市',6,11,12), (10,'寧波市',6,13,14);
SELECT id,name FROM region WHERE parent_id=0;(或 lft=1 根節點)SELECT id,name FROM region WHERE parent_id=?; -- 傳入一級 idSELECT parent.name FROM region AS node JOIN region AS parent ON node.lft BETWEEN parent.lft AND parent.rgt WHERE node.name = '杭州市' ORDER BY parent.lft;
(lft, rgt) 建立複合索引,加速 BETWEEN 範圍查詢。path VARCHAR(500) 存儲「1/6/9/」)或閉包表(ancestor, descendant, depth)輔助查詢。嵌套集合以空間換時間,將樹查詢轉為範圍查詢,是唯讀密集型樹形業務的高效之選。但在寫入頻繁場景下,維護成本指數級上升,請根據業務讀寫比謹慎選型。