in reply to Message Board Quandary, Revisited
I voted on nodes in your previous post regarding this, so I must have seen it, but it seems I didn't mention Celko's methods of storing such trees using SQL. I heartily recommend Celko's SQL for Smarties, which treats the matter. A smaller book on this specific topic is Trees and Hierarchies in SQL for Smarties, but that is mostly a rehash of his SQL column which are available on the web.
The idea that seems the one most convenient and transparent to me is to store the location of a node in the tree as a string which traces the path to the tree:
create table nodes ( node_id integer not null primary key, parent integer references node_id, message varchar(1024), location varchar(256) -- only 256 levels of replies and only 256 replies to a root node ); -- Let's create a discussion -- root level insert into nodes (node_id, message,location) values (1,'Hello',''); -- we count from 'A' to 'Z' in this simplicistic example insert into nodes (node_id, message,location) values (2,'Re: Hello','A +'); insert into nodes (node_id, message,location) values (3,'Re: Hello','B +'); insert into nodes (node_id, message,location) values (4,'Re: Re: Hello + (1)','AA'); insert into nodes (node_id, message,location) values (5,'Re: Re: Hello + (2)','AB'); insert into nodes (node_id, message,location) values (6,'Re: Re: Hello +!','BA');
If you want all children of the node number 2, you can run a LIKE query against your table and you will get back the list of nodes in one query:
select node_id, location from nodes where location like 'A%' order by location asc
This saves you the issuing of multiple queries against your database at the price of having a set limit on the number of replies to a node and at the cost of an (anchored) LIKE query. A LIKE query might or might not use an index, depending on your database, so you have to check that. I've demonstrated a very simplicistic approach of using alphabetic characters to denote the position in the tree. You can expand that approach by using multiple characters per depth (for example sprintf '%04d' if you want to keep ASCII), but the fixed limit will remain.
I found some articles by Celko on the subject and likely this Google query will turn up more...
|
|---|