聊聊mysql的樹形結構儲存及查詢

codecraft發表於2022-04-10

本文主要研究一下mysql的樹形結構儲存及查詢

儲存parent

這種方式就是每個節點儲存自己的parent_id資訊
  • 建表及資料準備

    CREATE TABLE `menu` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(50) NOT NULL,
    `parent_id` int(11) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;
    
    INSERT INTO `menu` (`id`, `name`, `parent_id`) VALUES
    (1, 'level1a',  0),
    (2, 'level1b', 0),
    (3, 'level2a-1a',1),
    (4, 'level2b-1a',1),
    (5, 'level2a-1b', 2),
    (6, 'level2b-1b', 2),
    (7, 'level3-2a1a', 3),
    (8, 'level3-2b1a', 4),
    (9, 'level3-2a1b', 5),
    (10, 'level3-2b1b', 6);
  • 查詢

    -- 查詢跟節點下的所有節點
    SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3
    FROM menu AS t1
    LEFT JOIN menu AS t2 ON t2.parent_id = t1.id
    LEFT JOIN menu AS t3 ON t3.parent_id = t2.id
    WHERE t1.name = 'level1a';
    
    +---------+------------+-------------+
    | lev1    | lev2       | lev3        |
    +---------+------------+-------------+
    | level1a | level2a-1a | level3-2a1a |
    | level1a | level2b-1a | level3-2b1a |
    +---------+------------+-------------+
    
    -- 查詢葉子節點
    SELECT t1.name FROM
    menu AS t1 LEFT JOIN menu as t2
    ON t1.id = t2.parent_id
    WHERE t2.id IS NULL;
    
    +-------------+
    | name        |
    +-------------+
    | level3-2a1a |
    | level3-2b1a |
    | level3-2a1b |
    | level3-2b1b |
    +-------------+
    儲存及修改上比較方便,就是要在sql裡頭查詢樹比較費勁,一般是載入到記憶體由應用自己構造

儲存path

這種方式在儲存parent的基礎上,額外儲存path,即從根節點到該節點的路徑
  • 建表及資料準備

    CREATE TABLE `menu_path` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(50) NOT NULL,
    `parent_id` int(11) NOT NULL DEFAULT '0',
    `path` varchar(255) NOT NULL DEFAULT '',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;
    
    INSERT INTO `menu_path` (`id`, `name`, `parent_id`, `path`) VALUES
    (1, 'level1a', 0, '1/'),
    (2, 'level1b', 0, '2/'),
    (3, 'level2a-1a',1, '1/3'),
    (4, 'level2b-1a',1, '1/4'),
    (5, 'level2a-1b', 2, '2/5'),
    (6, 'level2b-1b', 2, '2/6'),
    (7, 'level3-2a1a', 3, '1/3/7'),
    (8, 'level3-2b1a', 4, '1/4/8'),
    (9, 'level3-2a1b', 5, '2/5/9'),
    (10, 'level3-2b1b', 6, '2/6/10');
  • 查詢

    -- 查詢某個節點的所有子節點
    select * from menu_path where path like '1/%'
    +----+-------------+-----------+-------+
    | id | name        | parent_id | path  |
    +----+-------------+-----------+-------+
    | 1  | level1a     | 0         | 1/    |
    | 3  | level2a-1a  | 1         | 1/3   |
    | 4  | level2b-1a  | 1         | 1/4   |
    | 7  | level3-2a1a | 3         | 1/3/7 |
    | 8  | level3-2b1a | 4         | 1/4/8 |
    +----+-------------+-----------+-------+
    查詢某個節點及其子節點比較方面,就是修改比較費勁,特別是節點移動,所有子節點的path都得跟著修改

MPTT(Modified Preorder Tree Traversal)

不儲存parent_id,改為儲存lft,rgt,它們的值由樹的先序遍歷順序決定
  • 建表及資料準備

    CREATE TABLE `menu_preorder` (
    `id` int(11) NOT NULL,
    `name` varchar(50) NOT NULL,
    `lft` int(11) NOT NULL DEFAULT '0',
    `rgt` int(11) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;
    
                     1(level1a)14
           2(level2a)7                8(level2b)13
    3(level3a-2a)4 5(level3b-2a)6 9(level3c-2b)10 11(level3d-2b)12
    
    INSERT INTO `menu_preorder` (`id`, `name`, `lft`, `rgt`) VALUES
    (1, 'level1a', 1, 14),
    (2, 'level2a',2, 7),
    (3, 'level2b',8, 13),
    (4, 'level3a-2a', 3, 4),
    (5, 'level3b-2a', 5, 6),
    (6, 'level3c-2b', 9, 10),
    (7, 'level3d-2b', 11, 12);
    
    select * from menu_preorder
    +----+------------+-----+-----+
    | id | name       | lft | rgt |
    +----+------------+-----+-----+
    | 1  | level1a    | 1   | 14  |
    | 2  | level2a    | 2   | 7   |
    | 3  | level2b    | 8   | 13  |
    | 4  | level3a-2a | 3   | 4   |
    | 5  | level3b-2a | 5   | 6   |
    | 6  | level3c-2b | 9   | 10  |
    | 7  | level3d-2b | 11  | 12  |
    +----+------------+-----+-----+
  • 查詢

    -- 查詢某個節點及其子節點,比如level2b
    select * from menu_preorder where lft between 8 and 13
    +----+------------+-----+-----+
    | id | name       | lft | rgt |
    +----+------------+-----+-----+
    | 3  | level2b    | 8   | 13  |
    | 6  | level3c-2b | 9   | 10  |
    | 7  | level3d-2b | 11  | 12  |
    +----+------------+-----+-----+
    
    -- 查詢所有葉子節點
    SELECT name
    FROM menu_preorder
    WHERE rgt = lft + 1;
    
    +------------+
    | name       |
    +------------+
    | level3a-2a |
    | level3b-2a |
    | level3c-2b |
    | level3d-2b |
    +------------+
    
    -- 查詢某個節點及其父節點
    SELECT parent.*
    FROM menu_preorder AS node,
    menu_preorder AS parent
    WHERE node.lft BETWEEN parent.lft AND parent.rgt
    AND node.name = 'level2b'
    ORDER BY parent.lft;
    
    +----+---------+-----+-----+
    | id | name    | lft | rgt |
    +----+---------+-----+-----+
    | 1  | level1a | 1   | 14  |
    | 3  | level2b | 8   | 13  |
    +----+---------+-----+-----+
    
    -- 樹形結構展示
    SELECT CONCAT( REPEAT(' ', COUNT(parent.name) - 1), node.name) AS name
    FROM menu_preorder AS node,
    menu_preorder AS parent
    WHERE node.lft BETWEEN parent.lft AND parent.rgt
    GROUP BY node.name
    ORDER BY node.lft;
    
    +--------------+
    | name         |
    +--------------+
    | level1a      |
    |  level2a     |
    |   level3a-2a |
    |   level3b-2a |
    |  level2b     |
    |   level3c-2b |
    |   level3d-2b |
    +--------------+
好處是通過lft進行範圍(該節點的lft,rgt作為範圍)查詢就可以,缺點就是增刪節點導致很多節點的lft及rgt都要修改

小結

  • 儲存parent的方式最為場景,一般樹形結構資料量不大的話,直接在應用層記憶體構造樹形結構和搜尋
  • 儲存path的好處是可以藉助path來查詢節點及其子節點,缺點就是移動node需要級聯所有子節點的path,比較費勁
  • MPTT的方式好處是通過lft進行範圍(該節點的lft,rgt作為範圍)查詢就可以,缺點就是增刪節點導致很多節點的lft及rgt都要修改

doc

相關文章