开发者

Tree Structure With History in SQL Server

I'm not sure if it's a duplicate question, but I couldn't find anything on this subject.

What's the best way for storing tree structure in a table with ability to store history of changes in that structure.

Thank you for any help.

UPDATE:

I have Employees Table and I need to store the structure of branches, departments, sections, sub sections.. in a table. I need to store historical information on employees branches, departments, to be able to retrieve an employee's branch, department, section, sub section even if the structure has been changed.

UPDATE 2:

There's a solution to save the 开发者_如何学Pythonwhole structure in a history table on every change in the structure, but is that the best approach?

UPDATE 3:

There's also Orders Table. I must store employee's position, branch, department, section, sub section and other on every order. That's the main reason for storing history. It will be used very often. In another words I should be able to show db data for every past day.

UPDATE 4:

Maybe using hierarchyid is an option?

What if a node is renamed? What should I do, if I need the old name on old orders?


I think you are looking for something like this. It provides a complete tree structure. This is used for a directory, but it can be used for anything: divisions, departments, sections, etc.

The History is separate and it is best if you get your head aroun dthe Node structure before contemplating the history. For the History of any table, all that is required is the addition of a DateTime or TimeStamp column to the PK. The history stores before images of the current rows.

Functions such as (a) resolving the path of the tree and (b) finding the relevant history rows that were current at some point in time, are performed using pure SQL. With MS you can use recursive CTEs for (a), or write a simpler form in a stored proc.

For (b), I would implement a derived version of the table (Node, Department, whatever) that returns the rows that were current at the relevant point in time; then use that for the historic version of (a).

It is not necessary to copy and save the entire tree structure every time it changes.

Feel free to ask questions if you need any clarification.

Data Model

▶Tree Structure with History Data Model

Readers who are unfamiliar with the Relational Modelling Standard may find ▶IDEF1X Notation◀ useful.


Depending on how the historical information will be used will determine whether you need a temporal solution or simply an auditing solution. In a temporal solution, you would store a date range over which a given node applies in its given position and the "current" hierarchy is derived by using the current date and time to query for active nodes in order to report on the hierarchy. To say that this is a complicated solution is an understatement.

Given that we are talking about an employee hierarchy, my bet is that an auditing solution will suffice. In an auditing solution, you have one table(s) for the current hierarchy and store changes somewhere else that is accessible. It should be noted that the "somewhere else" doesn't necessarily need to be data. If changes to the company hierarchy are infrequent, then you could even use a seriously low-tech solution of creating a report (or series of reports) of the company hierarchy and store those in PDF format. When someone wanted to know what the hierarchy looked like last May, they could go find the corresponding PDF printout.

However, if it is desired to have the audit trail be queryable, then you could consider something like SQL Server 2008's Change Tracking feature or a third-party solution which does something similar.

Remember that there is more to the question of "best" than the structure itself. There is a cost-benefit analysis of how much effort is required vs. the functionality it provides. If stakeholders need to query for the hierarchy at any moment in time and it fluctuates frequently (wouldn't want to work there :)) then a temporal solution may be best but will be more costly to implement. If the hierarchy changes infrequently and granular auditing is not necessary, then I'd be inclined to simply store PDFs of the hierarchy on a periodic basis. If there is a real need to query for changes or granular auditing is needed, then I'd look at an auditing tool.

Change Tracking

Doing a quick search, here's a couple of third-party solutions for auditing:

ApexSQL

Lumigent Technologies


there are several approaches, however with your limited information I would say to use single table with parent relationship, for the history you can simply implement audit of the table

UPDATE: based on your new information I would not use a single table data store, it looks like your hierarchical structure is much more complex than a simple tree, also it looks like you have some well defined nodes of the structures especially at the top before getting into the sections and sub-sections; so multi-table relations might be a better fit;

as far as the audit tables, there are plenty of resources to find out what will work best for you, there are per row and per column audits, etc.


One thing to note is that you dont ahve to historize the tree structure. It always only grows, never gets smaller.

What chagnes is the USE of the nodes. Their data may change.

For eaxample a site.

/a/b/c

will be there forever. A may be a folder, b too, c a file. Later a may be a folter, b a tombstone (no data here at the moment) as c. But the tree in itself never changes.

Then, add a version number, and for every node a historiy / list of uses (node types) with start and possibly (can be null) end version.

The code for showing a version X then can build out the "real" tree for this moment easily.

To support moves of nodes, have a "rehook" type that indicates the noe was moved to another item at this version.

Voila.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜