
Joe Celko's Trees and Heirarchies in SQL for Smarties (Morgan Kaufmann) by Joe Celko
How many of us have sat down and written a Category Manager? I'd guess probably everyone reading this. How did you go about structuring that? If, like I did at the start, you used the "Parent -> Child" relationship model, then you ought to take a look at this book.
Joe Celko's Trees and Heirarchies in SQL for Smarties
Author: Joe Celko
Publisher: Morgan Kaufman
You see, Parent Child structures work well, up to a point. What is more, they are easy to implement and logical. Not many developers have trouble understanding what's going on when faced with a "parentRef" in a SQL table. But they have their downside as well. Sequencing them can be a nightmare, leading to expanded table columns holding "sequence" values. Grabbing entire trees can also be a problem, say for example you've got the following:
Item 1
Item 1.1
Item 1.2
Item 1.2.1
Item 1.2.1.1
Item 1.2.1.2
Item 1.2.1.3
Item 1.2.1.3.1
Item 1.2.1.4
Item 1.2.2
Item 1.2.3
Item 1.3
Item 2
Item 3
etc
This isn't a particular complex or large structure by any stretch of the imagination. But let's say you wish to count how many items there are under Item 1.2. By just counting the items that have a parent of 1.2 you'd only get a value of 3 back. In order to get the true value you need to recurse deeper into the set. If you're dealing with a very large set of data this becomes an expensive process, and so you start looking for work-arounds such as storing the maximum depths of given nodes, child node counts in summary tables, etc.
This book however provides a different method to handling the recursion issues that the Adjacency List Model above suffers from. It is called the "Nested Set Model". If you are already familiar with how this works, then you can save yourself £17 and skip this book entirely. For the rest of us, the information it holds provides a small revelation.
I do not wish to go into the details of the Nested Set Model here, after all that is what this book is for, but it may well open your eyes to a whole new way of thinking about categorisations and the management of them. There is a perfectly good Pear package all ready for you to use re: Nested sets, but this book will teach you exactly how it works, rather than just relying on blind faith. The SQL gets quite complex. If you use MySQL you'll have an especially hard time emulating most of it in anything less than MySQL v5 due to the stored procedures and events the example SQL uses. But really the golden nugget in this book isn't the SQL, it's the method. Once you've read and appreciated the simplicity of it, you will no doubt find your own way to implement it into your PHP code. By combining some of the elements of Nested Sets with the Adjancency Lists you can create a formidable and fast hierarchical system.
I have not looked online for nested set information for a while, so it's entirely possible that some online resources can explain it in detail, saving you the need to buy this book at all - especially when the vast majority of it will be useless from a PHP perspective. But if not, it's very cheap, very easy to read (you'll finish it in a day) and will have you thinking "ahh yes!" for certain.