parent-join 適用場景

Child documents can be added, changed, or deleted without affecting either the parent or other children. This is especially useful when child documents are large in number and need to be added or changed frequently.



Nested&Join父子文選型必須考慮性能問題。nested 類型檢索使得檢索效率慢幾倍,父子Join 類型檢索會使得檢索效率慢幾百倍。

The parent-child relationship is similar in nature to the nested model: both allows us to associate one entity with another. The difference is that, with nested objects, all entities live within the same document while, with parent-child, the parent and children are completely separate documents. However, it also is bound to some restrictions:

不同在於,nested objects 所有的實體在一個文檔中,而parent-child 父文檔和子文檔是在不同的文檔中。

Nested Object Parent-Child
保存在同一個文檔(document)中 保存在不同的文檔中
一個孩子對象可以有多個父對象 一個孩子對象不可以有多個父對象
查詢相對快 查詢很慢,因爲父子文檔保存在不同的文檔中
很容易維護多級網絡 很難維護多級網絡
nested級別的查詢很明確,使用簡單 當有多個父子關係的時候查詢變得複雜
可以查詢所有的數據 不能在一個查詢中同時查詢父級和子級文檔
每個父級對象有複製 不涉及數據重複
如果一個nested對象改變了,所有的父級對象都需要重建索引 不需要重建索引,因爲只維護了一個連接

Parent - Child 限制

The parent and child types must be different or parent-child relationships cannot be established between documents of the same type.


Parent and child documents must be indexed on the same shard. The parent ID is used as the routing value for the child, to ensure that the child is indexed on the same shard as the parent. This means that the same parent value needs to be provided when getting, deleting, or updating a child document.

父子文檔必須索引在同一個分片上。父文檔的 id 被用到索引子文檔,來確保父子文檔索引在同一個分片上。這意味着當獲取、刪除或者修改一個子文檔的時候需要提供相同的 parent 的值。


本文分享自微信公衆號 - 哥妞(gh_d18ec82f19ea)。
如有侵權,請聯繫 [email protected] 刪除。

還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.