SlideShare is now on Android. 15 million presentations at your fingertips.  Get the app

×
  • Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
 

Models for hierarchical data

by on May 20, 2010

  • 91,086 views

Tree-like data relationships are common, but working with trees in SQL usually requires awkward recursive queries. This talk describes alternative solutions in SQL, including:...

Tree-like data relationships are common, but working with trees in SQL usually requires awkward recursive queries. This talk describes alternative solutions in SQL, including:

- Adjacency List
- Path Enumeration
- Nested Sets
- Closure Table

Code examples will show using these designs in PHP, and offer guidelines for choosing one design over another.

Statistics

Views

Total Views
91,086
Views on SlideShare
77,226
Embed Views
13,860

Actions

Likes
122
Downloads
1,764
Comments
13

31 Embeds 13,860

http://habrahabr.ru 13319
http://www.slideshare.net 167
http://graylien.tumblr.com 84
http://oggettoweb.github.io 60
http://m.habrahabr.ru 56
http://www.developpez.net 25
http://ingenic.com 25
http://blog.agel-nash.ru 20
https://twitter.com 16
http://www.scoop.it 16
http://x443.wordpress.com 15
https://jabbr.net 9
https://si0.twimg.com 7
http://navidoo.ru 6
http://a0.twimg.com 6
https://twimg0-a.akamaihd.net 5
http://us-w1.rockmelt.com 5
http://hml-monqi.monqi.com.br 3
http://paper.li 2
https://www.google.ru 2
http://121.243.14.240 2
http://www.php-talks.com 1
https://uclaextension.blackboard.com 1
http://webcache.googleusercontent.com 1
http://www.widjer.ru 1
http://localhost 1
http://flyear.net 1
http://www.tinymce.com 1
http://me.zing.vn 1
http://twitter.com 1
http://www.google.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via SlideShare as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

110 of 13 previous next Post a comment

  • hemeshthakkar Hemesh Thakkar, Software Engineer at SMG Convonix How to get all the parents / ancestors of one particular comment using the adjacency list ? 7 months ago
    Are you sure you want to
    Your message goes here
    Processing…
  • stzoid stzoid I am happy to say I accidentally chose the Closure Table method on a recent project. I was looking around for alternatives to my design. 7 months ago
    Are you sure you want to
    Your message goes here
    Processing…
  • anotherjohndoe anotherjohndoe Firebird does suppot recursive CTEs. In fact it was the first open source RDBMS to support them (PostgreSQL was the second) 1 year ago
    Are you sure you want to
    Your message goes here
    Processing…
  • akhaisin Andrii Khaisin, Software developer at Provectus IT After a few days breaking my head at sql database and tree traversal I've found these slides. Naive approach which I tried to use is shown on the first pages.
    Thanks to author.
    2 years ago
    Are you sure you want to
    Your message goes here
    Processing…
  • RafisGaneyev Rafis Ganeyev at Московский технологический институт Thank you for presentantion.

    INSERT INTO Comments
    VALUES (8, 'Fran', 'I agree!');

    INSERT INTO TreePaths (ancestor, descendant)
    SELECT ancestor, 8 FROM TreePaths
    WHERE descendant = 8
    UNION ALL SELECT 8,8; -- add self

    But how to add child with level column?
    2 years ago
    Are you sure you want to
    Your message goes here
    Processing…
  • imme_emosol W.H. Fris, Student, Tutor, Developer on Web's Full Stack at Mediacollege Amsterdam, home<3 i think breadcrumbs are where i have been,
    not about how to get some place,
    i'd call that the route or path.
    2 years ago
    Are you sure you want to
    Your message goes here
    Processing…
  • TomWindsor Tomáš Windsor The closure table solution has however one drawback, and that is that it doesn't contain the information about order of individual records among others that have the same ancestor (in your example you could use timestamp column on the main table to sort the comments, but in general the hierarchy can have customizable order at each level and it is not that uncommon that you need to present the data in sorted way - then sorting them would mean many other queries, no?). To add that functionality, we would then probably have to add another column to closure table which would contain somehow encoded priority enumeration. 2 years ago
    Are you sure you want to
    Your message goes here
    Processing…
  • samsam9988 samsam9988 Hi

    I tried your script for retrieving immediate parent from a given child in a nested sets model, but it only returns a list of ancestors of the child.

    Here is my script:
    select parent.* from category_subcategories as c

    join category_subcategories as parent
    on (c.lft between parent.lft and parent.rgt)

    left outer join category_subcategories as in_between
    on (c.lft between in_between.lft and in_between.rgt
    and in_between.lft between parent.lft and parent.rgt)

    where c.category_name = 'Sofa';

    and here is the result:

    +-------------+---------------+------+------+
    | category_id | category_name | lft | rgt |
    +-------------+---------------+------+------+
    | 248 | Top | 0 | 19 |
    | 248 | Top | 0 | 19 |
    | 248 | Top | 0 | 19 |
    | 248 | Top | 0 | 19 |
    | 302 | AAA | 1 | 16 |
    | 302 | AAA | 1 | 16 |
    | 302 | AAA | 1 | 16 |
    | 599 | Houseware | 2 | 11 |
    | 599 | Houseware | 2 | 11 |
    | 669 | Sofa | 9 | 10 |
    +-------------+---------------+------+------+
    10 rows in set (0.05 sec)

    How can I retrieve Sofa's parent - Houseware?
    3 years ago
    Are you sure you want to
    Your message goes here
    Processing…
  • billkarwin Karwin Software Solutions LLC at Karwin Software Solutions LLC @milehighrockstar: Thanks for your comment! I think I addressed a similar scenario in my blog:
    http://karwin.blogspot.com/2010/03/rendering-trees-with-closure-tables.html
    3 years ago
    Are you sure you want to
    Your message goes here
    Processing…
  • milehighrockstar milehighrockstar Correction on the last row:
    1,1,0
    1,2,1
    2,3,1
    1,4,1
    4,5,1
    4,6,1
    6,7,1
    3 years ago
    Are you sure you want to
    Your message goes here
    Processing…

110 of 13 previous next

Post Comment
Edit your comment

Models for hierarchical data Models for hierarchical data Presentation Transcript