2012-01-19 18 views
0

以前被問及類似的問題,但沒有人回答,所以我已經重新考慮了這個問題,並提出了類似/不同的問題。樹 - 如何找出使用mysql如果節點的後代完成

我有一個以父/根(頂部)應用程序開始的進程。根應用程序然後衍生子應用程序,它也可以派生後代應用程序。這可以繼續多個級別。然後每個級別可以是節點或葉子。節點可以有後代。葉子沒有衍生的孩子/後代應用程序。

在流程開始時,應用程序知道關卡的數量。該過程也是結構化的,因此每個子應用程序都可以在完成時更新tbl,並具有自己的ID以及parentID。

因此,當整個過程運行時,結果數據是一個分層樹。

我想弄清楚如何能夠查看樹中給定的項目/節點,並確定後代應用程序是否完整。

我正試圖在mysql中完成此操作。我不熟悉存儲過程/子選擇。我已經看到了一些討論這個問題的在線論文/網站,但是我沒有看到任何關於我的問題的論點。

尋找一個mysql大師來幫助我弄清楚這個問題。

謝謝!

--------------------------------- 

The sample tree would look like: 

spawn 
3 levels 
a - 3 copies of b 
b - 3 copies of c 


        a(1) 
         | 
--------------------------------------------------------------------- 
      |b(1)     |b(2)       |b(3) 
-------------------   -------------------   -------------------- 
|c(1) |c(2) |c(3)  c(1) |c(2) |c(3)  |c(1) |c(2) |c(3) 


so we have a total of 12 crawls/fetches 

the levels 
a 
b 
c 

the (parent/child) levelRelationships 
"",a 
a,b 
b,c 

start level 
a (parent/top) 
end level 
c (leaf) 


operational process: 

an app spawns either no child app, a single child app, or multiple child app(s) 
an app that spawns children is a node 
an app that spawns no children is a leaf 
there is no guarantee that an app at a given level, will stop operation 
before an app at a lower level started by it's parent 
each child app can set a tbl with a status when it completes 
when each child app is complete, it generates a "level/complete" status 
    which is stored in a levelStatusTBL 

at the start of the root/top level process: 
-the tree can have multiple/unknown levels 
-each child app can spawn an unknown number of children 


issue... 
how to algorithmically determine when all the descendants of a root/top level function have completed? 
how to algorithmically determine when all the descendants of a node have completed 

,我正在考慮樣本tbls是:

CREATE TABLE `crawlNodeChildrenCountTBL` (
    `rootID` varchar(100) NOT NULL DEFAULT '', 
    `uCrawlID` varchar(100) NOT NULL DEFAULT '', 
    `childCount` int(5) NOT NULL DEFAULT 0, 
    `ID` int(10) NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`ID`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

CREATE TABLE `EdgeNodeCheckTBL` (
    `CollegeID` varchar(100) NOT NULL DEFAULT '', 
    `rootID` varchar(100) NOT NULL DEFAULT '', 
    `parentLevel` varchar(100) NOT NULL DEFAULT '', 
    `Level` varchar(100) NOT NULL DEFAULT '', 
    `nodeType` int(5) NOT NULL DEFAULT 0,  
    `masterParseInputUUID` varchar(100) NOT NULL DEFAULT '', 
    `parentSetupPreComboID` varchar(100) NOT NULL DEFAULT '', 
    `SetupPreComboChildStatusID` varchar(100) NOT NULL DEFAULT '', 
    `ID` int(10) NOT NULL AUTO_INCREMENT, 
    UNIQUE KEY `ID` (`ID`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 


EdgeNodeCheckTBL.SetupPreComboChildStatusID is the baseID 
EdgeNodeCheckTBL.parentSetupPreComboID is the parentID of SetupPreComboChildStatusID 

this is used to implement the standard child/parent relationship tbl 

回答

0

這是一個真正的數據算法問題。基本的問題是,將父ID存儲在關係數據庫的子記錄中將需要遞歸查詢。如果您可以在存儲過程或其他語言中這樣做,那麼這是一種有效的方法。

將樹存儲在關係數據庫中的更好方法是使用nested set model。這個想法是,每個節點都有一個Left Id和一個Right Id,這個簡單的序列就是每個節點使用前序遍歷訪問的時候的序列。當離開頂部節點沿着樹離開時,左側標識被設置;當朝樹頂部節點上行時設置正確標識。在樹被修改時,您還必須更新數字。

這個結構給你的好處是你不需要遞歸查詢來檢查或更新樹。它還鼓勵您將對樹的修改隔離到一個位置,以便您可以正確更新左側和右側ID。

維基百科的文章有更多的細節。

+0

嗨staticsan。應該補充一點,我們在研究鏈表/鄰接模型時考察了嵌套集方法。嵌套方法不適用於我們的情況,因爲隨着產生的應用程序(以及樹)數量的增長,樹會不斷增長。所以嵌套圖必須不斷重建。該過程可能會增長到1000個頂級父節點,每個節點都有1000個節點作爲它們自己的樹,儘管樹級可能最多隻有7-8級深度。 –

+0

@staticsan:嵌套集合適用於靜態和小型樹木,但是當您擁有一棵巨大的樹並且變化很大時,它非常昂貴。 – Bytemain

+0

@David:不,嵌套集對於動態變化的樹是理想的,我認爲它適用於中等大小的樹 - 對於大型樹而言不是理想的(數百萬個節點 - 注意正確用法葉也是節點) - 但是根據你的原始問題,每個節點與一個沒有道理的過程相關。 – symcbean

相關問題