<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Recursion-less storage of hierarchical data in a relational database</title>
	<atom:link href="http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/</link>
	<description>The website of Alan deLevie</description>
	<pubDate>Thu, 11 Mar 2010 20:32:11 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Twitter Trackbacks for Alan deLevie - Recursion-less storage of hierarchical data in a relational database [alandelevie.com] on Topsy.com</title>
		<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/#comment-551</link>
		<dc:creator>Twitter Trackbacks for Alan deLevie - Recursion-less storage of hierarchical data in a relational database [alandelevie.com] on Topsy.com</dc:creator>
		<pubDate>Tue, 09 Feb 2010 16:29:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.alandelevie.com/?p=11#comment-551</guid>
		<description>[...] Alan deLevie - Recursion-less storage of hierarchical data in a relational database  www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database &#8211; view page &#8211; cached  One of the main problems with using a relational database such as MySQL is that it can be tricky to store and retrieve hierarchical information. [...]</description>
		<content:encoded><![CDATA[<p>[...] Alan deLevie - Recursion-less storage of hierarchical data in a relational database  <a href="http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database"  rel="nofollow">http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database</a> &ndash; view page &ndash; cached  One of the main problems with using a relational database such as MySQL is that it can be tricky to store and retrieve hierarchical information. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: buyset</title>
		<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/#comment-523</link>
		<dc:creator>buyset</dc:creator>
		<pubDate>Fri, 13 Nov 2009 09:24:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.alandelevie.com/?p=11#comment-523</guid>
		<description>I need the script on how to add the numbers 1 to finish under Method 2: the modified preorder tree traversal algorithm. How can i add the left and right node?</description>
		<content:encoded><![CDATA[<p>I need the script on how to add the numbers 1 to finish under Method 2: the modified preorder tree traversal algorithm. How can i add the left and right node?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mindstorm</title>
		<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/#comment-522</link>
		<dc:creator>mindstorm</dc:creator>
		<pubDate>Tue, 10 Nov 2009 17:38:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.alandelevie.com/?p=11#comment-522</guid>
		<description>patented??! i used this method 6 years back while writing an application which i'm selling now before i even read anything about storing hierarchical/tree data in a database. This is like patenting walking. Holy cow.</description>
		<content:encoded><![CDATA[<p>patented??! i used this method 6 years back while writing an application which i&#8217;m selling now before i even read anything about storing hierarchical/tree data in a database. This is like patenting walking. Holy cow.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/#comment-521</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Fri, 06 Nov 2009 23:46:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.alandelevie.com/?p=11#comment-521</guid>
		<description>I keep hearing people talking about reducing overhead by loading the whole mess into an array with a single database query - but it seems to me that that all of the sorting and looping you'd have to do to organize the information into a sensible array would make up for most of the performance you gained (Maybe I'm underestimating the overhead of creating a new mysql query, however.  I know very little about that sort of thing).  

On top of that - if you're just loading it all in with a single query, and THEN transferring it to an array, don't you lose the advantage of using mysql in the first place (that is, allowing you to use their methods for sorting and filtering)?  Seems like you'd be reinventing the wheel.

Lastly - if you're going to just load everything up all at once - why not just store the whole mess as a serialized array in a single field in the database?  Then, at the very least, you could keep yourself from doing all that looping and sorting when you pulled the info out.

This whole thing is a mess.  I need very simple hierarchy for about 350 items.  Because of some other circumstances, the adjacency method is too slow.  I find myself trying to wrap my head around MPTTA just for this?  Ugh.  Surely there is a simpler way.</description>
		<content:encoded><![CDATA[<p>I keep hearing people talking about reducing overhead by loading the whole mess into an array with a single database query - but it seems to me that that all of the sorting and looping you&#8217;d have to do to organize the information into a sensible array would make up for most of the performance you gained (Maybe I&#8217;m underestimating the overhead of creating a new mysql query, however.  I know very little about that sort of thing).  </p>
<p>On top of that - if you&#8217;re just loading it all in with a single query, and THEN transferring it to an array, don&#8217;t you lose the advantage of using mysql in the first place (that is, allowing you to use their methods for sorting and filtering)?  Seems like you&#8217;d be reinventing the wheel.</p>
<p>Lastly - if you&#8217;re going to just load everything up all at once - why not just store the whole mess as a serialized array in a single field in the database?  Then, at the very least, you could keep yourself from doing all that looping and sorting when you pulled the info out.</p>
<p>This whole thing is a mess.  I need very simple hierarchy for about 350 items.  Because of some other circumstances, the adjacency method is too slow.  I find myself trying to wrap my head around MPTTA just for this?  Ugh.  Surely there is a simpler way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim</title>
		<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/#comment-516</link>
		<dc:creator>Tim</dc:creator>
		<pubDate>Thu, 01 Oct 2009 22:02:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.alandelevie.com/?p=11#comment-516</guid>
		<description>3 comments about the Chandler method:  1 -  from a database normalization standpoint this is a poor idea.  It does not allow for the growth of the heirarchy.  There are very few (I cannot think of any) real world examples where you can guarantee the hierarchy will not expand.  2 - The patent is rediculous from the standpoint that others have used this method long before David supposedly "invented" it.  And 3 - his patent is easily circumvented by using a data type other than a number to designate the levels since a number is what he specifies in the claim.</description>
		<content:encoded><![CDATA[<p>3 comments about the Chandler method:  1 -  from a database normalization standpoint this is a poor idea.  It does not allow for the growth of the heirarchy.  There are very few (I cannot think of any) real world examples where you can guarantee the hierarchy will not expand.  2 - The patent is rediculous from the standpoint that others have used this method long before David supposedly &#8220;invented&#8221; it.  And 3 - his patent is easily circumvented by using a data type other than a number to designate the levels since a number is what he specifies in the claim.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Madis</title>
		<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/#comment-515</link>
		<dc:creator>Madis</dc:creator>
		<pubDate>Fri, 25 Sep 2009 21:14:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.alandelevie.com/?p=11#comment-515</guid>
		<description>anyway, i could post a solution in form of a tutorial, if anybody is interested and i receive some feedback on that matter. The hypothetical title would be "How to remove a database overhead from recursive tree method"</description>
		<content:encoded><![CDATA[<p>anyway, i could post a solution in form of a tutorial, if anybody is interested and i receive some feedback on that matter. The hypothetical title would be &#8220;How to remove a database overhead from recursive tree method&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Madis</title>
		<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/#comment-514</link>
		<dc:creator>Madis</dc:creator>
		<pubDate>Fri, 25 Sep 2009 21:02:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.alandelevie.com/?p=11#comment-514</guid>
		<description>and i must agree - how dumb can a patent official be, if he/she grants a patent to what everyone knows as basic way of listing something

1. Preface
2. Fruits
2.1 Apple
2.2 Pear
2.3 Some Very Exotic stuff that make you puke


eeee ....  - why you call it a chandler method? it's a simple nested-numerated list been around before the computer was invented ...

what do you want to patent next? scratching your back?

this is why i greet European parliament for voting against software patents in EU.</description>
		<content:encoded><![CDATA[<p>and i must agree - how dumb can a patent official be, if he/she grants a patent to what everyone knows as basic way of listing something</p>
<p>1. Preface<br />
2. Fruits<br />
2.1 Apple<br />
2.2 Pear<br />
2.3 Some Very Exotic stuff that make you puke</p>
<p>eeee &#8230;.  - why you call it a chandler method? it&#8217;s a simple nested-numerated list been around before the computer was invented &#8230;</p>
<p>what do you want to patent next? scratching your back?</p>
<p>this is why i greet European parliament for voting against software patents in EU.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Madis</title>
		<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/#comment-513</link>
		<dc:creator>Madis</dc:creator>
		<pubDate>Fri, 25 Sep 2009 20:48:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.alandelevie.com/?p=11#comment-513</guid>
		<description>to: PMC 

Actually, no mather the hype, it's pretty simple. You "improve" the recursive method by taking out the database overhead (the cornerstone of any critics on the recursive method - tooo many querys). 

do only one query to database and select ALL THE TREE,  store in array and use recursrion there. so query everything and parse it to a array like this

$mytree[parent] = array([item_id]=&#62;1, ...)

in other words - two dimensional tree where first level array has parent_id as key, and second level array has item id as array key.

important thing to note is, that you must order the data from database by parent, so the first element is root - eg $mytree[0]

after that it's really easy to use recursion in order to display data... 

why ? because (IMHO) what the recursive method provides, that most other methods lack - it is very hard to break the rock-solid parent_id &#62; child_id relations. You have to do something really dumb to mess it up.

example is a cutout from a class, maybe it illustrates the idea better the i can explain. basically you emulate the database parent&#62;child relation in a code level - and you do only one query.

	private function loadTreeData()
	{
		$sql = 'SELECT *FROM '.$this-&#62;tblTreeItems .'
		WHERE tree_id = \''.(INT)$this-&#62;treeId.'\'
		ORDER BY item_parent_id, item_sequence_number ASC
		';
		$this-&#62;db-&#62;execQuery($sql);
		return $this-&#62;db-&#62;fetchAll('item_id'); 
		
	}
	
	private function processTreeData()
	{
		if (is_array($this-&#62;treeDataRaw)) {
			foreach ($this-&#62;treeDataRaw AS $key =&#62; $value)
			{
				$this-&#62;treeDataArray[$value['item_parent_id']][$key] = $value;
			}

		}			
	
	}
	protected function hasChildren($parent_id)
	{
		if (array_key_exists($parent_id, $this-&#62;treeDataArray)) {
			return true;			
		}
		else 
		{
			return false;
		}
	}
	
	protected function getChildren($parent_id)
	{
		return $this-&#62;treeDataArray[$parent_id];
	}
	
	
	/* recursive ul/li list for debugging */
	public function printUlList($parent_id)
	{
		print '';
		if ($this-&#62;hasChildren($parent_id)) {
			$children = $this-&#62;getChildren($parent_id);
			foreach ($children AS $child)
			{
				print ''.$child['item_name'].'';
				$this-&#62;printUlList($child['item_id']);
			}
			
			
		}
		print '';
		
	}</description>
		<content:encoded><![CDATA[<p>to: PMC </p>
<p>Actually, no mather the hype, it&#8217;s pretty simple. You &#8220;improve&#8221; the recursive method by taking out the database overhead (the cornerstone of any critics on the recursive method - tooo many querys). </p>
<p>do only one query to database and select ALL THE TREE,  store in array and use recursrion there. so query everything and parse it to a array like this</p>
<p>$mytree[parent] = array([item_id]=&gt;1, &#8230;)</p>
<p>in other words - two dimensional tree where first level array has parent_id as key, and second level array has item id as array key.</p>
<p>important thing to note is, that you must order the data from database by parent, so the first element is root - eg $mytree[0]</p>
<p>after that it&#8217;s really easy to use recursion in order to display data&#8230; </p>
<p>why ? because (IMHO) what the recursive method provides, that most other methods lack - it is very hard to break the rock-solid parent_id &gt; child_id relations. You have to do something really dumb to mess it up.</p>
<p>example is a cutout from a class, maybe it illustrates the idea better the i can explain. basically you emulate the database parent&gt;child relation in a code level - and you do only one query.</p>
<p>	private function loadTreeData()<br />
	{<br />
		$sql = &#8216;SELECT *FROM &#8216;.$this-&gt;tblTreeItems .&#8217;<br />
		WHERE tree_id = \&#8221;.(INT)$this-&gt;treeId.&#8217;\&#8217;<br />
		ORDER BY item_parent_id, item_sequence_number ASC<br />
		&#8216;;<br />
		$this-&gt;db-&gt;execQuery($sql);<br />
		return $this-&gt;db-&gt;fetchAll(&#8217;item_id&#8217;); </p>
<p>	}</p>
<p>	private function processTreeData()<br />
	{<br />
		if (is_array($this-&gt;treeDataRaw)) {<br />
			foreach ($this-&gt;treeDataRaw AS $key =&gt; $value)<br />
			{<br />
				$this-&gt;treeDataArray[$value['item_parent_id']][$key] = $value;<br />
			}</p>
<p>		}			</p>
<p>	}<br />
	protected function hasChildren($parent_id)<br />
	{<br />
		if (array_key_exists($parent_id, $this-&gt;treeDataArray)) {<br />
			return true;<br />
		}<br />
		else<br />
		{<br />
			return false;<br />
		}<br />
	}</p>
<p>	protected function getChildren($parent_id)<br />
	{<br />
		return $this-&gt;treeDataArray[$parent_id];<br />
	}</p>
<p>	/* recursive ul/li list for debugging */<br />
	public function printUlList($parent_id)<br />
	{<br />
		print &#8221;;<br />
		if ($this-&gt;hasChildren($parent_id)) {<br />
			$children = $this-&gt;getChildren($parent_id);<br />
			foreach ($children AS $child)<br />
			{<br />
				print &#8221;.$child['item_name'].&#8221;;<br />
				$this-&gt;printUlList($child['item_id']);<br />
			}</p>
<p>		}<br />
		print &#8221;;</p>
<p>	}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roberto Fonseca</title>
		<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/#comment-507</link>
		<dc:creator>Roberto Fonseca</dc:creator>
		<pubDate>Fri, 21 Aug 2009 17:02:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.alandelevie.com/?p=11#comment-507</guid>
		<description>Hi, I'm tryin to do that, and I have a table with id/name/parent - but I'd like to return a tree with characteres like *level1 **level1A **level1B *level2 **level2a ***level2a1. OffCourse the LEVEL(N)(A) are the name. But I've getting no sucess just using MySQL without and code. Do ya know how could I do that?</description>
		<content:encoded><![CDATA[<p>Hi, I&#8217;m tryin to do that, and I have a table with id/name/parent - but I&#8217;d like to return a tree with characteres like *level1 **level1A **level1B *level2 **level2a ***level2a1. OffCourse the LEVEL(N)(A) are the name. But I&#8217;ve getting no sucess just using MySQL without and code. Do ya know how could I do that?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://www.alandelevie.com/2008/07/12/recursion-less-storage-of-hierarchical-data-in-a-relational-database/#comment-506</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Fri, 24 Jul 2009 04:53:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.alandelevie.com/?p=11#comment-506</guid>
		<description>Thanks for the alternative solution. I'm loving the creativity.</description>
		<content:encoded><![CDATA[<p>Thanks for the alternative solution. I&#8217;m loving the creativity.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
