Re: Question about grouping, catagorization, etc...

From: Andy Hassall (andy_at_andyh.co.uk)
Date: 07/31/04


Date: Fri, 30 Jul 2004 23:14:01 +0100

On Thu, 29 Jul 2004 22:07:04 -0700, "Anthony M. Saffer"
<anthony@NOSPAM.opensource-strategies.com> wrote:

>I'm stuck on a problem that I'm sure isn't that hard to solve. In fact, I've
>read the solution a few months ago but can't seem to find it again now (when
>I really need it). I am hoping someone can clue me in. Here's the problem:
>
>Let's say I have a table called 'tblCategories' which contains 5 records and
>is structured like this
>
>tblCategories
> ID CATEGORY_NAME SUB_CATEGORY_OF
> 1 Automobiles
> 2 Planes
> 3 Buick 1
> 4 Cessna 2
> 5 Trains
>
>Now, as you can see the "Buick" category is both a "upper level" category
>(which means it might have sub categories as well) and a sub category of
>category 1 (Automobiles). You will also notice that category 4 (Cessna) is
>the same.
>
>My problem is that I need to order these into
>category/subcategory/sub-sub-category links similar to the way Yahoo does
>their directory. So, on the output page, the output might look like this:
>
>Automobiles
> Buick
>Planes
> Cessna
>Trains
>
>Basically, I want the user to be able to "drill down" through categories.
>Can anyone clue me in on how to accomplish this?

 Depends on your database, and what you want to optimise it for - easy updates,
or quick reads of the tree. Odds are it'll be read much more than written to.

 Options include:

(1) A recursive function that fetches all children of a category, then all
children of then, issuing multiple queries until it's traversed the tree. Works
on your current schema, but is going to result in an awful lot of database
roundtrips as your tree grows.

(2) Use a hierarchical query SQL extension if your database supports it, e.g.
CONNECT BY in Oracle. As I work mostly with Oracle, this is what I tend to use.
Not many databases have extensions like this; MySQL certainly doesn't. Whilst
it's really quite similar to (1), it's all done in the database in one round
trip, which can give considerable savings.

(3) Read the whole lot in one go, and get PHP to sort out the tree structure.
Unlikely to scale well at all.

(4) Start denormalising your schema to make selects easier, at the cost of
making inserts more costly. Here's my take on the issue from a while ago:

http://groups.google.com/groups?selm=3ls1gukloommab2omash5lnovh5nun4ufa%404ax.com

(5) Use one of the clever tree storage schemes that are floating around, such
as Joe Celko's "nested sets" or "materialised path". Some
sample hits from Google on those terms:

http://www.dbazine.com/tropashko4.shtml
http://www.codeproject.com/database/nestedsets.asp

--
Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk         / http://www.andyhsoftware.co.uk/space


Relevant Pages

  • Re: JTree & database: ID column ?
    ... > from database and for it I use ID1 column which is unique ... > and every number in a code represents the place in a parent node. ... And if you can construct a tree ... reverse-index is NOT an auto-inserted id value, although it has to be unique ...
    (comp.lang.java.gui)
  • Re: Parsing and storing formulas
    ... you could easily store different views/stored ... >I was wondering how I can parse a mathematical formula in a storable way. ... > be stored for in the database. ... > The second idea is to represent the formula as a tree. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Managing Genealogy Relationships in FMP 8.5
    ... the database (the file has tables for managing photos, documents, ... family tree, he's variously a brother, husband, grandfather, son, ... but a complete map is hella more efficient. ... Replace a Sort field to 1. ...
    (comp.databases.filemaker)
  • Re: Unique identifier in every treenode?
    ... The important part about that code is the recursion, which will be a common element in any implementation that enumerates the tree completely.. ... Then when saving, you'd include for each node the Guid of the _parent_ of that node. ... But my understanding is that you can create a database where one of the columns is a unique identifier, and optionally where the unique identifier is automatically generated when you add a record to the database. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What so special about PostgreSQL and other RDBMS?
    ... That's exactly the link the licence agreement for the database points to when it ... comes to what wecan expect for paying support. ... > "Oracle may provide additional releases or versions of its programs ... If the requirements are volatile I'd do a long term contract detailing what ...
    (comp.lang.php)