[翻译]  Best Relational DataBase Representation Of Time Bound Hierarchies

[CHINESE]  时间绑定层次结构的最佳关系数据库表示


What in everyone's opinion is the best representation for a time-bound hierarchy in SQL?

大家认为什么是SQL中有时间限制的层次结构的最佳表示?

What I mean by this is:
- On any given date you have a normal tree hierarchy
- This hierarchy can change from day to date
- Each child still only has one parent on any given date

我的意思是: - 在任何给定的日期,你有一个普通的树层次结构 - 这个层次结构可以从一天到另一个变化 - 每个孩子在任何给定的日期仍然只有一个父

Day 1...

第1天......

Business
 |
 |-Joe
 |  |-Happy
 |  |-Sneezy
 |  |-Doc(*)
 |
 |-Moe
    |-Bashfull
    |-Sleepy

Day 2...

第2天......

Business
 |
 |-Joe
 |  |-Happy
 |  |-Sneezy
 |
 |-Moe
    |-Doc(*)
    |-Bashfull
    |-Sleepy

At any time, a child can join the hierarchy for the first time, or leave the hierarchy completely. (For example, new employees, and retired employees.)

孩子可以在任何时候第一次加入层次结构,或完全离开层次结构。 (例如,新员工和退休员工。)

The main considerations:

主要考虑因素:

  • Updating the hierarchy
  • 更新层次结构
  • Viewing the whole hierarchy across a date range
  • 查看日期范围内的整个层次结构
  • Reporting on whole sub-trees within the hierarchy
  • 报告层次结构中的整个子树
  • Reporting on whole sub-trees across a date range
  • 报告日期范围内的整个子树

I know how I do it at present, but am intrigued as to how other people may do it :)

我知道我现在是怎么做的,但我对其他人怎么做可能很感兴趣:)

EDIT

编辑

I naively assumed a few considerations so will be more explicit...

我天真地假设了一些考虑,所以会更明确......

  • Each 'team' or 'person' will have a unique ID in a dimension table elsewhere
  • 每个“团队”或“人员”在其他地方的维度表中都会有唯一的ID
  • Other fact tables will use those IDs (storing performance metrics, for example)
  • 其他事实表将使用这些ID(例如,存储性能指标)
  • The structure needs to facilitate historical reporting across date ranges
  • 该结构需要促进跨日期范围的历史报告
  • Use of ETL or triggers to maintain alternative structures Is an option
  • 使用ETL或触发器来维护替代结构是一种选择

The generic nature is most important (forming just one part of a generic relational mode), combined with ease of use for driving report (for any part of the tree across any range of dates) and the ability to be updated reliably.

通用性质是最重要的(仅形成通用关系模式的一部分),结合易于使用的驱动报告(对于任何日期范围的树的任何部分)以及可靠地更新的能力。

4 个解决方案

#1


7  

There are several different books of relevance here - one set is for 'temporal databases', and the other for 'hierarchical structures in RDBMS'.

这里有几本不同的相关书籍 - 一组用于'时态数据库',另一组用于“RDBMS中的层次结构”。

The tricky parts of your question, it seems to me, are:

在我看来,你问题的棘手部分是:

  • Viewing the whole hierarchy across a date range

    查看日期范围内的整个层次结构

  • Reporting on whole sub-trees across a date range

    报告日期范围内的整个子树

The other items are, if not straight-forward, then manageable using the techniques outlined in the books, and along the lines suggested in other answers. Part of the problem is understanding what those two bullet points mean. In one sense, they are 'the same'; the 'whole hierarchy' is just a special case of 'whole sub-trees'. But the deeper question is 'how do you want to demonstrate - visualize, represent - the changes in the hierarchy over time?' Are you seeking to compare the states at the start and end times, or are you seeking to see the intermediate changes too? How do you want to represent the moves of an individual within a hierarchy?

其他项目,如果不是直截了当,则可以使用书中概述的技术进行管理,并按照其他答案中建议的方式进行管理。部分问题是理解这两个要点的含义。从某种意义上说,它们是“相同的”; “整体层次结构”只是“整个子树”的一个特例。但更深层次的问题是“你想如何展示 - 可视化,代表 - 层次结构随时间的变化?”您是想在开始和结束时间比较状态,还是在寻找中间变化?您想如何表示层次结构中个人的行为?

More questions than answers - but I hope the pointers are some help.

问题多于答案 - 但我希望指针有所帮助。

#2


0  

A couple of flat tables can work here. For each row, we need columns ID, Name, ParentID, and InactivatedDatetime (which defaults to null). Set the datetime for the old Doc belonging to Joe indicating that that record is no longer valid and move it off to an archive table (for cleanliness), and then create a new row (a near copy of the original row) for a new Doc with Moe's ID as the ParentID. The drawback with this approach is that the person being moved must get a new ID, which may not be convenient.

几张平台可以在这里工作。对于每一行,我们需要列ID,名称,ParentID和InactivatedDatetime(默认为null)。设置属于Joe的旧Doc的日期时间,指示该记录不再有效,并将其移至存档表(清洁度),然后为新Doc创建新行(原始行的近似副本)使用Moe的ID作为ParentID。这种方法的缺点是被移动的人必须得到一个新的ID,这可能不方便。

#3


0  

I can think of a couple of reasonable solutions, depending on how your data is being used and how it changes.

我可以考虑一些合理的解决方案,具体取决于您的数据使用方式及其变化方式。

1) Assuming today's hierarchy is the most important. I'd store today's hierarchy with a conventional ParentId column in each record. For previous versions of the hierarchy I'd have a history table of

1)假设今天的等级是最重要的。我会在今天的层次结构中存储每个记录中的传统ParentId列。对于以前版本的层次结构,我有一个历史表

ItemId, ParentId, ValidFromDate, ValidToDate

Any time the hierarchy changes, you add a new row to the history table.

每次更改层次结构时,都会向历史表中添加一个新行。

2) If any/all of the hierarchies are of equal importance, I'd store a base line hierarchy and then implement a hierarchy transaction table.

2)如果任何/所有层次结构具有相同的重要性,我将存储基线层次结构,然后实现层次结构事务表。

TransactionId, ItemId, Action (Move/Delete/Add), DateTime, OldParentId, NewParentId

#4


0  

table item(id, ...)

table item_link(parent_item, child_item, from_date, until_date)

The links will store the representation of the tree for a certain time

链接将存储树的表示一段时间

This structure represents a network instead of a plain hierarchy but it supports moving things in a hierarchy but also look back in time. Some things need to be checked in application logic is to disallow joe being linked at different places in the hierarchy at the sametime.

此结构表示网络而不是普通层次结构,但它支持在层次结构中移动事物,但也可以回顾过去。在应用程序逻辑中需要检查的一些事情是禁止在同一时间在层次结构中的不同位置链接joe。

Reporting is relatively easy with connect by prior clause (in oracle)

通过先前条款(在oracle中)连接报告相对容易

Other details can be related to item or even item link if it is to specify additional data on the relation.

如果要指定关系的其他数据,则其他详细信息可以与项目甚至项目链接相关。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号