If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Hierarchy structures in databases

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-14-08, 10:46
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Hierarchy structures in databases

I always thought hierarchies were quite useful in a databases ie good for representing structures of offices, people, product hierarchies etc. After suggesting them as a soloution to two posts this week ( 1 2 ) I find there appears to be some reluctance to using hierarchies - can anyone say why?

They are obviously a bit more involved than a simple join but it's not rocket science - I seem to remember Oracle even has extra functionality built into it's SQL to help. I do accept it's a bit more difficult to code, I also accept it's slightly slower than a simple join but these types of structure provide a lot of power.

Is there a better way of handling hierarchy information or do people just avoid these types of structures?

Mike
Reply With Quote
  #2 (permalink)  
Old 01-14-08, 11:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
there is only a reluctance to use a hammer when clearly some other tool is more appropriate

also, i should like to point out that there is more than one way of representing a hierarchy

when you suggest parentOfficeId or childName, parentName, these are examples of the adjacency list data model for hierarchies

there are other ways, including nested sets and materialized paths

the "some reluctance" that you may have detected was merely me suggesting that a hierarchy was over-complicating matters in a particular situation where no hierarchy was needed at all
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-14-08, 12:22
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
As you say, there are as many ways to represent just about anything but I'm genuinly curious why a hierarchical method isn't the optimal method of storing what appears to be hierarchical data. I did google nested sets and materialized paths and could see that they may work but find it very difficult to believe they're a simpler solution.

Could you explain what I'm missing?

Mike
Reply With Quote
  #4 (permalink)  
Old 01-14-08, 12:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
that's the whole point, mike

just because something appears (emphasis: to you) to be hierarchical data, this does not imply that one should use a hierarchical model to store it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-14-08, 12:42
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I accept your point but what would be a better answer than the hierarchy? To me the original problem is screaming out "hierarchy". Even the nested sets and materialized paths you propose appear to be just different ways of representing hierarchical data. I'm confused - how should the data be represented?
Reply With Quote
  #6 (permalink)  
Old 01-14-08, 12:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by mike_bike_kite
Even the nested sets and materialized paths you propose appear to be just different ways of representing hierarchical data.
yep, that's what they are

why use a hierarchy when you don't really need one?

as i said in that thread...

in the real world, every different variation has its own "stock keeping unit" and barcode (do a search for GTIN and GS1)

there's no product hierarchy in the GS1 database, and i should know, i've worked on it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 01-14-08, 15:06
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Must admit I haven't produced any product databases so I'll have to bow out to your experience. We certainly use a lot of hierarchies in financial reporting systems though and they seem to work well. If you cache the hierarchy data in a "flat" table then the queries are no more complex than a standard query and obviously faster than calculating the hierarchy each time.

What are the basic criteria for deciding when to use the nested sets and materialized paths over standard hierarchies?

Mike
Reply With Quote
  #8 (permalink)  
Old 01-14-08, 15:15
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
sorry, can't answer your last question
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 01-14-08, 16:23
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Quote:
Originally Posted by mike_bike_kite
What are the basic criteria for deciding when to use the nested sets and materialized paths over standard hierarchies?
Here are some good rules of thumb on which model to implement:

Use multiple tables when you want to ensure that the company cannot replace you with a DBA making under $40,000 per year.

Use the Adjency Model when you want to ensure that the company cannot replace you with a DBA making under $70,000 per year.

Use Nested Sets when you want to ensure that the company cannot replace you with a DBA making under $100,000 per year.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #10 (permalink)  
Old 01-14-08, 20:07
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Very good!

The funny thing is I found some SQL code yesterday that I just couldn't work out (and the current team couldn't explain what it was doing either). Having read up on this stuff I now realise it's a nested set hierarchy - how about that for a coincidence! This forum is very educational

Mike
Reply With Quote
  #11 (permalink)  
Old 01-15-08, 03:51
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
I had heard, but do not know, that nested sets have an a degree of exposure that is disproportionate to their use in the real world.
In other words (English this time) - they get discussed a lot but rarely make it into production. Anyone any experience to the contrary?
Reply With Quote
  #12 (permalink)  
Old 01-15-08, 06:01
pavel.urbancik pavel.urbancik is offline
Registered User
 
Join Date: Dec 2007
Posts: 2
Quote:
Originally Posted by mike_bike_kite
What are the basic criteria for deciding when to use the nested sets and materialized paths over standard hierarchies?
Mike
I've used both models extensively,so here is quick summary:

Nested Sets:
- space efficient (2 ints per node)
- unlimited tree depth
- very fast access to direct/all ancestors
- very fast access to direct/all descendants
- *VERY* slow inserts / updates / deletes

Materialized path:
- lots of space wasted
- limited tree depth
- fast access to all ancestors
- moderately fast access to direct ancestors
- fast access to all descendants
- moderately fast access to direct descendants
- fast inserts / updates / deletes

I prefer Nested Sets for mostly static hierarchies with lots of nodes, space/speed advantage over Materialized Path is noticable.

For everything else I recommend using Materialized Path.

Pavel Urbancik
Reply With Quote
  #13 (permalink)  
Old 01-15-08, 06:31
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Thanks for the response!

How do these methods compare with:
  • the rather bog standard hierarchic table with parent and child fields
  • the addition of a cached hierarchic table where accessing a parent in the cache table will result in all the child records without looping etc.

Are there issues with the readability of the code - the SQL code doesn't look at all obvious to me - perhaps senility is finally setting in.

Mike
Reply With Quote
  #14 (permalink)  
Old 01-15-08, 09:18
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
I think the decision is between Nested Set and Adjacency models. I've never used Materialized Path, and doubt that I ever will.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #15 (permalink)  
Old 01-15-08, 09:24
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Quote:
Originally Posted by mike_bike_kite
Are there issues with the readability of the code - the SQL code doesn't look at all obvious to me - perhaps senility is finally setting in.
Have you tried coding binary searches with SQLSVR 2005's new Common Table Expressions? That will determine whether you are still a young enough dog to learn new tricks...it took me several tries to get the hang of it.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On