modeling trees

Upload: junkyard

Post on 07-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Modeling Trees

    1/20

    Modeling Trees in SQL

    Richard Broersma Jr.Mangan Inc. System Integrator

  • 8/4/2019 Modeling Trees

    2/20

    From a data modeling perspective,

    what is a Tree ?

    A specialized form of Graph (data structure).

  • 8/4/2019 Modeling Trees

    3/20

    From a data modeling perspective,

    what is a Graph ?

    For a set of Entities( Vertexes ), a set of

    relationships( edges ) can be defined to

    represent all associations between entities.

  • 8/4/2019 Modeling Trees

    4/20

    Simple Graph Illustration

    Notice: Provides a flexible way to relate entities Multiple relationship paths exist for entities Graph Analysis is inefficient using SQL

  • 8/4/2019 Modeling Trees

    5/20

    Graph Analysis In Practice

    Which path between two terminals: has the smallest transit time has the least number of stops passes through a certain terminal

  • 8/4/2019 Modeling Trees

    6/20

    Okay but what about TREES!

    Notice:

    Have a root that all entities relate to Limits singular relationships between entities Relationships from root to leaf are hierarchical

    constraints make SQL analysis performant

  • 8/4/2019 Modeling Trees

    7/20

    Trees In Practice: Org Chart

    Models the Has zero or more: Who is the manager of any given employee Which emps directly report to a manager Which emps ultimately managed by a manager

  • 8/4/2019 Modeling Trees

    8/20

    Trees In Practice: Assembly (BOM)

    Models the Contains zero or more: What sub assemblies compose an assembly What is an assembly ultimately composed of

  • 8/4/2019 Modeling Trees

    9/20

    Logic Entity Relationship Diagram

    Barker'sNotation/Style

    Chen like Style

  • 8/4/2019 Modeling Trees

    10/20

    Good Practice Tree Design

    Liberal add Check Constraints to eliminate

    possible sources of tree corruptionDecouple entity and relationship in separate

    tables. Eliminates Entity/Relationship Denormalization Allows multiple tree relationship tables to exist

    to represent different types of relationships

  • 8/4/2019 Modeling Trees

    11/20

  • 8/4/2019 Modeling Trees

    12/20

    Physical Designs

    Adjacency ListModel

    A.K.A. BOM Model

    Nested Set ModelA.K.A. Celko Tree

    Path EnumerationModel

    (Impetus ForPostgreSQL LTREEContrib Module)

  • 8/4/2019 Modeling Trees

    13/20

  • 8/4/2019 Modeling Trees

    14/20

    Adjacency List Model

    CREATE TABLE OrgChart(emp VARCHAR(10) NOT NULL PRIMARY KEYREFERENCES Personnel( emp ),

    boss VARCHAR(10) null means rootREFERENCES Personnel( emp ),

    ... );

    Albert NULL

    Bert Albert

    Chuck Albert

    Donna Chuck

    Eddie Chuck

    Fred Chuck

    emp boss

  • 8/4/2019 Modeling Trees

    15/20

    Adjacency List Model

    Performs well with Inserts & Deletes.

    Using SQL92, can only return at most one levelof the tree per statement (until PG 8.4 CTE) and

    can perform badly.

    With SQL03, With Recursive makes ALM a goodchoice.

  • 8/4/2019 Modeling Trees

    16/20

    Nested Set Model

    CREATE TABLE OrgChart(lft INTEGER NOT NULL,rgt INTEGER NOT NULL,

    CONSTRAINT lftlessthanrgtCHECK( lft < rgt ),

    emp VARCHAR(10) NOT NULL PRIMARY KEYREFERENCES Personnel( emp )

    ... );

    1 12 Albert

    2 3 Bert

    4 11 Chuck

    5 6 Donna

    7 8 Eddie

    9 10 Fred

    lft rgt emp

  • 8/4/2019 Modeling Trees

    17/20

    Nested Set Model

    Performs badly with numerous Inserts & Deletes. Selects Perform very well Using SQL92, easily returns all levels of the tree

    per statement. (hard to limit to immediate levels) Nature of Nested Set allow for easy analysis of

    tree structure ex. WHERE lft >= 4 AND rgt

  • 8/4/2019 Modeling Trees

    18/20

    Path Enumeration Model

    CREATE TABLE OrgChart(path VARCHAR NOT NULL,emp VARCHAR(10) NOT NULL PRIMARY KEY

    REFERENCES Personnel( emp ),CONSTRAINT validPathCHECK( path LIKE '%' || emp ),

    ... );

    path

    Albert Albert

    Bert

    Chuck

    Donna

    Eddie

    Fred

    emp

    AlbertBert

    AlbertChuck

    AlbertChuckDonna

    AlbertChuckEddie

    AlbertChuckFred

  • 8/4/2019 Modeling Trees

    19/20

    Path Enumeration Model

    Performs well numerous Inserts & Deletes. Selects performs very but has typical LIKE '%.%'

    gotchas. (LTREE improves this with gist support) Using SQL92, easily returns all levels of the tree

    per statement and can limit to immediate levels Analyzing tree structure is complicated ex. WHERE emp LIKE 'AlbertChuck%'

  • 8/4/2019 Modeling Trees

    20/20

    References

    Batini, Carol, Stefano Ceri, and Shamkant B. Navathe. Conceptual Database Design : An Entity Relationship Approach. Boston:

    Benjamin Cummings Company, 1991.

    Celko, Joe. Joe Celko's SQL for Smarties : Advanced SQL Programming. 3rd ed. Greensboro: Morgan Kaufmann, 2005.

    Celko, Joe. Joe Celko's Thinking in Sets : Auxiliary, Temporal, and Virtual Tables in SQL. New York: Elsevier Science &

    Technology Books, 2008.

    Celko, Joe. Joe Celko's Trees and Hierarchies in SQL for Smarties. Greensboro: Morgan Kaufmann, 2004.

    Douglas, Korry. PostgreSQL. 2nd ed. Indianapolis: Sams, 2005.