![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I realize this is a classic problem, but I'm a NOVICE after all. I want to represent hierarchal topics (just like dmoz.org). I've seen two ways to represent the data. Both are described at http://www.sitepoint.com/article/1105/1 And in another article by Joe Celko about using Modified Preorder Trees. I'm leaning toward using the simpler "adjacency list model" where each node (topic) in the tree just lists its parent. create table topic ( topic_id serial PRIMARY KEY, name varchar(64), parent_id int -- possible to use "REFERENCES topic" but allow NULL? ) The problem becomes then how to find the path from a given node to the root node. I'm working with perl and currently what I'm doing is a recursive call to the database. That's going to be slow if I have to look up many of those. |
#3
| |||
| |||
|
|
You have a fundamental problem if you want to go from high-level to low-level if you only store the parent_id (from low-level to high-level). [in a booming voice]: Feed your head. Good luck. |
|
In any case, throw an example on paper and see why your scheme will not work. You need a better reference than SITEPOINT for what you want to do... What they say does not apply to you. |
|
http://www.sitepoint.com/article/1105/1 |
#4
| |||
| |||
|
|
I realize this is a classic problem, but I'm a NOVICE after all. I want to represent hierarchal topics (just like dmoz.org). I've seen two ways to represent the data. Both are described at http://www.sitepoint.com/article/1105/1 And in another article by Joe Celko about using Modified Preorder Trees. I'm leaning toward using the simpler "adjacency list model" where each node (topic) in the tree just lists its parent. create table topic ( topic_id serial PRIMARY KEY, name varchar(64), parent_id int -- possible to use "REFERENCES topic" but allow NULL? ) The problem becomes then how to find the path from a given node to the root node. I'm working with perl and currently what I'm doing is a recursive call to the database. That's going to be slow if I have to look up many of those. My question is this: is there a way to get Postgresql to do this recursive query for me? My other question is how to get from a topics path to a topic node id. That is, can someone suggest a way to find the topic id if you have a path like: /top/Computers/Software/Operating_Systems/Open_Source/ Thanks, -- Bill Moseley moseley (AT) hank (DOT) org ---------------------------(end of broadcast)--------------------------- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html |
#5
| |||
| |||
|
|
I didn't receive much feedback from this post. Would psql-general be a better list to post this question? Or is there a better place to ask a general database design question? |
#6
| |||
| |||
|
|
Bill Moseley wrote: I didn't receive much feedback from this post. Would psql-general be a better list to post this question? Or is there a better place to ask a general database design question? Try searching the mail archives for pgsql-general and maybe pgsql-sql first. This topic has been discussed in great depth more than once, and there are many expressed opinions on the "best" way to tackle it. |
|
One solution you could look at is connectby() in contrib/tablefunc. Another is contrib/ltree. And as I said, many others discussed in the archives. |
#7
| |||
| |||
|
|
I realize this is a classic problem, but I'm a NOVICE after all. I want to represent hierarchal topics (just like dmoz.org). I've seen two ways to represent the data. Both are described at http://www.sitepoint.com/article/1105/1 And in another article by Joe Celko about using Modified Preorder Trees. I'm leaning toward using the simpler "adjacency list model" where each node (topic) in the tree just lists its parent. create table topic ( topic_id serial PRIMARY KEY, name varchar(64), parent_id int -- possible to use "REFERENCES topic" but allow NULL? ) The problem becomes then how to find the path from a given node to the root node. I'm working with perl and currently what I'm doing is a recursive call to the database. That's going to be slow if I have to look up many of those. My question is this: is there a way to get Postgresql to do this recursive query for me? My other question is how to get from a topics path to a topic node id. That is, can someone suggest a way to find the topic id if you have a path like: /top/Computers/Software/Operating_Systems/Open_Source/ Thanks, |
![]() |
| Thread Tools | |
| Display Modes | |
| |