2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | www.mysql.com 1 Managing Hierarchical Data in MySQL Mike Hillyer - MySQL AB.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

Hierarchies & Trees in SQL by Joe Celko copyright 2008.
Trees Types and Operations
Introduction to Trees Chapter 6 Objectives
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Class 8: Trees. cis 335 Fall 2001 Barry Cohen Definitions n A tree is a nonlinear hierarchical structure (more than one successor) n Consists of nodes.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Table & Query Design for Hierarchical Data without CONNECT-BY -- A Path Code Approach Charles Yu Database Architect Elance Inc. Elance Inc.
COSC2007 Data Structures II
More Trees Multiway Trees and 2-4 Trees. Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store.
CS261 Data Structures Trees Introduction and Applications.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
2005­02-02 | Database Normalization | © MySQL AB 2005 | 1 Database Normalization PHP Quebec 2005 Mike Hillyer – MySQL AB.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
Topic 17 Introduction to Trees
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Data Structures TREES.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Bottom-Up Red-Black Trees Top-down red-black trees require O(log n) rotations per insert/delete. Color flips cheaper than rotations. Priority search trees.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
This Lecture Intro to Tree ADT Terminologies Tree Types Tree Traversals Binary Search Tree Expression Trees.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
2006­02-08 | Database Normalization | © MySQL AB 2006 | 1 An Introduction to Database Normalization Mike Hillyer – MySQL AB.
XML DOM Week 11 Web site:
CSC 213 – Large Scale Programming. Trees  Represent hierarchical relationships  Parent-child basis of this abstract data type  Real-world example:
CSE 373 Data Structures Lecture 7
BCA-II Data Structure Using C
G64ADS Advanced Data Structures
B/B+ Trees 4.7.
Querying Hierarchical Data
Binary search tree. Removing a node
Week 6 - Wednesday CS221.
SNS COLLEGE OF TECHNOLOGY (Autonomous ) COIMBATORE-35
Tree.
Section 8.1 Trees.
Tree traversal from Introduction to Trees Chapter 6 Objectives
Lecture 18. Basics and types of Trees
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
COSC2100: Data Structures and Algorithms
Data Structures Balanced Trees CSCI
TCSS 342, Winter 2006 Lecture Notes
Trees and Binary Trees.
MANAGING DATA RESOURCES
General Trees & Binary Trees
Database.
Chapter 14: Augmenting Data Structures
B-Tree.
Name: Player number Session 1 Session 2 Session 3 Session 4 Table Seat
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Non-Linear Structures
Podcast Ch18a Title: Overview of Binary Search Trees
Chapter 20: Binary Trees.
VISION Advanced Training Welcome to… Mike Reitmeyer
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 1 Managing Hierarchical Data in MySQL Mike Hillyer - MySQL AB

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 2 About Me Member of the MySQL AB Documentation Team MySQL Core and Pro Certified MySQL Expert at Resident MySQL Expert at SearchDatabase.com Mike Hillyer, BSc

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 3 About You Currently use MySQL? Another RDBMS? Manage hierarchical data? Will be managing hierarchical data? Know what hierarchical data is? How many of you…

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 4 About This Session What is hierarchical data? The Adjacency List Model The Nested Set Model Querying the Nested Set Model –Adding New Nodes –Deleting Nodes –Additional Resources

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 5 What is Hierarchical Data? Data that maintains a parent-child relationship Nodes have zero or one parent, zero to many children Only the root node has no parent Various types of data –Mailing list/forum threads –Organizational charts –Content management categories –Product categories

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 6 Product Categories

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 7 The Adjacency List Model | category_id | name | parent | | 1 | ELECTRONICS | NULL | | 2 | TELEVISIONS | 1 | | 3 | TUBE | 2 | | 4 | LCD | 2 | | 5 | PLASMA | 2 | | 6 | PORTABLE ELECTRONICS | 1 | | 7 | MP3 PLAYERS | 6 | | 8 | FLASH | 7 | | 9 | CD PLAYERS | 6 | | 10 | 2 WAY RADIOS | 6 |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 8 Full Tree SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4 FROM category AS t1 LEFT JOIN category AS t2 ON t2.parent = t1.category_id LEFT JOIN category AS t3 ON t3.parent = t2.category_id LEFT JOIN category AS t4 ON t4.parent = t3.category_id WHERE t1.name = 'ELECTRONICS'; | lev1 | lev2 | lev3 | lev4 | | ELECTRONICS | TELEVISIONS | TUBE | NULL | | ELECTRONICS | TELEVISIONS | LCD | NULL | | ELECTRONICS | TELEVISIONS | PLASMA | NULL | | ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS | FLASH | | ELECTRONICS | PORTABLE ELECTRONICS | CD PLAYERS | NULL | | ELECTRONICS | PORTABLE ELECTRONICS | 2 WAY RADIOS | NULL | rows in set (0.00 sec)

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 9 Leaf Nodes SELECT t1.name FROM category AS t1 LEFT JOIN category as t2 ON t1.category_id = t2.parent WHERE t2.category_id IS NULL; | name | | TUBE | | LCD | | PLASMA | | FLASH | | CD PLAYERS | | 2 WAY RADIOS |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 10 Single Path SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4 FROM category AS t1 LEFT JOIN category AS t2 ON t2.parent = t1.category_id LEFT JOIN category AS t3 ON t3.parent = t2.category_id LEFT JOIN category AS t4 ON t4.parent = t3.category_id WHERE t1.name = 'ELECTRONICS' AND t4.name = 'FLASH'; | lev1 | lev2 | lev3 | lev4 | | ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS | FLASH | row in set (0.01 sec)

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 11 Limitations of Adjacency Lists Adjacency list model not normalized. Deleting nodes can orphan children. Querying paths and trees requires multiple self-joins. Procedural code required for most use-cases.

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 12 Nested Sets

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 13 Nested Set Table | category_id | name | lft | rgt | | 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 |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 14 Numbered Sets

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 15 Numbered Tree

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 16 Full Tree SELECT node.name FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND parent.name = 'ELECTRONICS' ORDER BY node.lft; | name | | ELECTRONICS | | TELEVISIONS | | TUBE | | LCD | | PLASMA | | PORTABLE ELECTRONICS | | MP3 PLAYERS | | FLASH | | CD PLAYERS | | 2 WAY RADIOS |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 17 Leaf Nodes SELECT name FROM nested_category WHERE rgt = lft + 1; | name | | TUBE | | LCD | | PLASMA | | FLASH | | CD PLAYERS | | 2 WAY RADIOS |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 18 Node Depth SELECT node.name, (COUNT(parent.name) - 1) AS depth FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt GROUP BY node.name ORDER BY node.lft; | name | depth | | ELECTRONICS | 0 | | TELEVISIONS | 1 | | TUBE | 2 | | LCD | 2 | | PLASMA | 2 | | PORTABLE ELECTRONICS | 1 | | MP3 PLAYERS | 2 | | FLASH | 3 | | CD PLAYERS | 2 | | 2 WAY RADIOS | 2 |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 19 Indented Nodes SELECT CONCAT( REPEAT(' ', COUNT(parent.name) - 1), node.name) AS name FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt GROUP BY node.name ORDER BY node.lft; | name | | ELECTRONICS | | TELEVISIONS | | TUBE | | LCD | | PLASMA | | PORTABLE ELECTRONICS | | MP3 PLAYERS | | FLASH | | CD PLAYERS | | 2 WAY RADIOS |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 20 Depth of a Sub-Tree SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth FROM nested_category AS node, nested_category AS parent, nested_category AS sub_parent, ( SELECT node.name, (COUNT(parent.name) - 1) AS depth FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.name = 'PORTABLE ELECTRONICS' GROUP BY node.name ORDER BY node.lft )AS sub_tree WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt AND sub_parent.name = sub_tree.name GROUP BY node.name ORDER BY node.lft;

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 21 Depth of a Sub-Tree - II | name | depth | | PORTABLE ELECTRONICS | 0 | | MP3 PLAYERS | 1 | | FLASH | 2 | | CD PLAYERS | 1 | | 2 WAY RADIOS | 1 |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 22 Immediate Subordinates GROUP BY node.name HAVING depth <= 1 ORDER BY node.lft; | name | depth | | PORTABLE ELECTRONICS | 0 | | MP3 PLAYERS | 1 | | CD PLAYERS | 1 | | 2 WAY RADIOS | 1 |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 23 Aggregate Functions SELECT * FROM product; | product_id | name | category_id | | 1 | 20" TV | 3 | | 2 | 36" TV | 3 | | 3 | Super-LCD 42" | 4 | | 4 | Ultra-Plasma 62" | 5 | | 5 | Value Plasma 38" | 5 | | 6 | Power-MP3 128mb | 7 | | 7 | Super-Shuffle 1gb | 8 | | 8 | Porta CD | 9 | | 9 | CD To go! | 9 | | 10 | Family Talk 360 | 10 |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 24 Aggregate Functions - II SELECT parent.name, COUNT(product.name) FROM nested_category AS node, nested_category AS parent, product WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.category_id = product.category_id GROUP BY parent.name ORDER BY node.lft; | name | COUNT(product.name) | | ELECTRONICS | 10 | | TELEVISIONS | 5 | | TUBE | 2 | | LCD | 1 | | PLASMA | 2 | | PORTABLE ELECTRONICS | 5 | | MP3 PLAYERS | 2 | | FLASH | 1 | | CD PLAYERS | 2 | | 2 WAY RADIOS | 1 |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 25 Adding Nodes

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 26 Adding a Sibling LOCK TABLE nested_category WRITE; := rgt FROM nested_category WHERE name = 'TELEVISIONS'; UPDATE nested_category SET rgt = rgt + 2 WHERE rgt UPDATE nested_category SET lft = lft + 2 WHERE lft INSERT INTO nested_category(name, lft, rgt) VALUES('GAME + + 2); UNLOCK TABLES; | name | | ELECTRONICS | | TELEVISIONS | | TUBE | | LCD | | PLASMA | | GAME CONSOLES | | PORTABLE ELECTRONICS | | MP3 PLAYERS | | FLASH | | CD PLAYERS | | 2 WAY RADIOS |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 27 Adding a Child LOCK TABLE nested_category WRITE; := lft FROM nested_category WHERE name = '2 WAY RADIOS'; UPDATE nested_category SET rgt = rgt + 2 WHERE rgt UPDATE nested_category SET lft = lft + 2 WHERE lft INSERT INTO nested_category(name, lft, rgt) + + 2); UNLOCK TABLES; | name | | ELECTRONICS | | TELEVISIONS | | TUBE | | LCD | | PLASMA | | GAME CONSOLES | | PORTABLE ELECTRONICS | | MP3 PLAYERS | | FLASH | | CD PLAYERS | | 2 WAY RADIOS | | FRS |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 28 Deleting a Leaf Node LOCK TABLE nested_category WRITE; := := := rgt - lft + 1 FROM nested_category WHERE name = 'GAME CONSOLES'; DELETE FROM nested_category WHERE lft UPDATE nested_category SET rgt = rgt WHERE rgt UPDATE nested_category SET lft = lft WHERE lft UNLOCK TABLES; | name | | ELECTRONICS | | TELEVISIONS | | TUBE | | LCD | | PLASMA | | PORTABLE ELECTRONICS | | MP3 PLAYERS | | FLASH | | CD PLAYERS | | 2 WAY RADIOS | | FRS |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 29 Deleting a Parent and all Children LOCK TABLE nested_category WRITE; := := := rgt - lft + 1 FROM nested_category WHERE name = 'MP3 PLAYERS'; DELETE FROM nested_category WHERE lft UPDATE nested_category SET rgt = rgt WHERE rgt UPDATE nested_category SET lft = lft WHERE lft UNLOCK TABLES; | name | | ELECTRONICS | | TELEVISIONS | | TUBE | | LCD | | PLASMA | | PORTABLE ELECTRONICS | | CD PLAYERS | | 2 WAY RADIOS | | FRS |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 30 Deleting a Parent and Promoting all Children LOCK TABLE nested_category WRITE; := := := rgt - lft + 1 FROM nested_category WHERE name = 'PORTABLE ELECTRONICS'; DELETE FROM nested_category WHERE lft UPDATE nested_category SET rgt = rgt - 1, lft = lft - 1 WHERE lft UPDATE nested_category SET rgt = rgt - 2 WHERE rgt UPDATE nested_category SET lft = lft - 2 WHERE lft UNLOCK TABLES; | name | | ELECTRONICS | | TELEVISIONS | | TUBE | | LCD | | PLASMA | | CD PLAYERS | | 2 WAY RADIOS | | FRS |

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 31 Advanced Use Cases Move an entire sub-tree Handle high insert/delete volumes –Calculate gaps –Use floats –See the resources next slide

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 32 Credit & Resources Joe Celko's Trees and Hierarchies in SQL for Smarties

2006­03-08 | Managing Hierarchical Data | © MySQL AB 2005 | 33 Conclusion Questions?