Querying Hierarchical Data

Slides:



Advertisements
Similar presentations
The Hierarchical Model
Advertisements

Hierarchies & Trees in SQL by Joe Celko copyright 2008.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
P2P Course, Structured systems 1 Introduction (26/10/05)
HOW TO OPTIMIZE A HIERARCHY IN SQL SERVER Louis Davidson (drsql.org)
Binary Trees Chapter 6.
Table & Query Design for Hierarchical Data without CONNECT-BY -- A Path Code Approach Charles Yu Database Architect Elance Inc. Elance Inc.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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.
ICOM 4035 – Data Structures Lecture 13 – Trees ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Example: Taxonomy of Organisms Hierarchy of categories: –Kingdom - phylum – class – order – family – genus - species –How would you design a relational.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Hierarchical Retrieval Fresher Learning Program December, 2011.
Managing Hierarchical Data in a Relational Database.
Overview of Security Investments in SQL Server 2016 and Azure SQL Database Jamey Johnston 1/15/2016Security Investments in SQL Server 2016 and Azure SQL.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Non Linear Data Structure
CSCE 210 Data Structures and Algorithms
Enterprise Row Level Security: SQL Server 2016 and Azure SQL DB
MySQL Subquery Source: Dev.MySql.com
Learning indexing concepts with an analog phone book
Fundamentals of Programming II Introduction to Trees
Problems with Linked List (as we’ve seen so far…)
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
Tree.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CMSC 341 Introduction to Trees.
Lecture 18. Basics and types of Trees
Program based on pointers in C.
Binary Trees, Binary Search Trees
Data Structures and Database Applications Binary Trees in C#
Entity Based Staging SQL Server 2012 Tyler Graham
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
Introduction lecture1.
General Trees & Binary Trees
Overview of Security Investments
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Week nine-ten: Trees Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Trees Definitions Implementation Traversals K-ary Trees
Trees CMSC 202, Version 5/02.
Trees (Part 1, Theoretical)
A Robust Data Structure
CMSC 202 Trees.
Enterprise RLS in SQL Server in Power BI
Binary Trees, Binary Search Trees
Trees.
Announcements Prelim 1 on Tuesday! A4 will be posted today
分级取回数据 Schedule: Timing Topic 30 minutes Lecture 20 minutes Practice
Important Problem Types and Fundamental Data Structures
Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Trees.
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Querying Hierarchical Data Trees Everywhere! Querying Hierarchical Data

About me (Ben Thul) Database Engineer for SurveyMonkey MCITP – Administrator/Developer Contact Info e: ben.thul@spartansql.com t: @spartansql w: http://www.spartansql.com

Why do I care? As it turns out, trees are good for modeling certain real-world phenomena Matri- or Patrilineal family trees Taxonomies (e.g. “Domain, Kingdom, Phylum, etc…”) Organizational Charts Hierarchical Categories

Why do I care? – cont. Once the hierarchy is modeled, it should be easy to answer questions like: What member(s) are related (either as ancestor, descendent, or sibling)? How far down the tree am I (i.e. some notion of “level”)?

What is a tree? For the purposes of this discussion, a tree is a defined set of parent/child relationships amongst a group of members in which the following are true No member is parent to itself One member is said to be superior to all other members. That is, all members can trace their lineage back to a single member Every member has at most one parent

A Tree - Visualized

For the math nerds mathematicians These are directed, acyclical, non-weighted graphs where each node has at most one edge going into it.

How do I model this? A common method is to use what is referred to as adjacency i.e. Add a column to each record that says which record is the immediate predecessor of this record There are various techniques of traversing the records all the way up and all the way down to find all ancestors and descendants

How do I model this? – An Example ID ParentID 1 NULL 2 12 112 1112 61299

The Bad/Old Days

Hope you like JOINs! Prior to SQL 2005, you could query this using UNION’d joins It was actually pretty efficient (for finding the lineage of a given ID)!  Not so great for getting “related” rows But writing it was tedious.  And error prone.  The general pattern is as follows Find the root element, select it For each successive level, union the above with Find the previous level’s members Join with their immediate descendants Rinse/lather/repeat for as many levels as you have

Demo Time Pre-2005 Style

A Renaissance

Recursive Queries With SQL 2005, recursive queries were introduced Similar in spirit to the last style, except that we can teach a robot how to do those joins

Recursive Queries - Example WITH cte as ( -- get the “root” ID select ID, ParentID from dbo.yourTable where ParentID is null union all -- get the children of the parents we’ve found so far select child.ID, child.ParentID from dbo.yourTable as child join cte as parent on child.ParentID = parent.ID ) select * from cte;

Recursive Queries – Continued As compared with the last style, we need only express the logic once, so much less error prone Works well if the data is indeed acyclical (i.e. no a → b → … → a) Automatically adjusts depth based on new data As compared with the JOIN style

Demo Time Recursive Queries

Your Future is Bright

Introducing the hierarchyid hierarchyid is a CLR datatype that was introduced in SQL 2008 Encodes a position within a hierarchy Provides methods for common operation IsDescendant – tests whether one node is a descendant of another GetAncestor(n) – walks the tree going up, getting the nth ancestor GetReparentedValue – moves a node from one parent to another Indexable!

What does it look like? Just looking at the value (i.e. select h from table) will yield a varbinary-looking value (e.g. 0x5D5E00E7B21FF800001F123F000006ECCC) You can look at a more readable version by calling the ToString() method on it (e.g. /1/10/92/919/9184/12345/) You can also specify your values in the human-readable version above for the purposes of equality testing, inserts, updates, etc. Implicit conversion will take care of things for you

Demo Time hierarchyid queries

Citations http://sqlblog.com/blogs/adam_machanic/archive/2015/04/07/re- inventing-the-recursive-cte.aspx – Script for sample data https://technet.microsoft.com/en-us/library/bb677212(v=sql.105).aspx – Working with hierarchical data https://msdn.microsoft.com/en-us/library/bb677193.aspx - hierarchyid method reference https://commons.wikimedia.org/wiki/File:Frogner_Park_Trees.JPG https://pixabay.com/en/caveman-primeval-primitive-man-159359/ https://pixabay.com/en/leonardo-da-vinci-vitruvian-man-1125056/ https://www.flickr.com/photos/buckaroobay/3721809183 https://github.com/ben-thul/HierarchyCLR - CLR source code