NOTE: This is actually a database of a forum.
I am stuck in an Interesting problem. I created a database with the following fields
ID (primary),
Data,
ParentID
ParentID is the ID of some other row in the same table. However, this parent child relationship can extend upto few levels. The topmost elements has 0 as parentID.
The database is quite large (500 MB), and querying everytime for the root element for a row will be too time consuming. What I want to do here is, add another column rootID. So if a particular data is 3 levels deep, it should have the ID of root (0 level) element in this column.
Can anyone help me out with the query syntax here?
I’ll assume that root elements have
ID = RootID. Since adding the root IDs is a one-off thing, I’d just start out with(after creating the new column, of course) and then run something like
repeatedly until you see zero rows updated. (And later when you add a new row, you just set RootID to that of the parent, if it has one, or to itself — you don’t need to ascend to the root).