Den*_*rdy 17 sql database postgresql database-design anchor-modeling
我正在考虑实现对象版本控制,需要同时拥有实时和草稿对象,并且可以使用来自某人经验的见解,因为我开始怀疑它是否有可能没有可能可怕的黑客攻击.
为了示例,我将其分解为带有标签的帖子,但我的用例更为一般(涉及缓慢变化的维度 - http://en.wikipedia.org/wiki/Slowly_changing_dimension).
假设您有一个posts表,一个tags表和一个post2tag表:
posts (
id
)
tags (
id
)
post2tag (
post_id fkey posts(id),
tag_id fkey tags(id)
)
Run Code Online (Sandbox Code Playgroud)
我需要一些东西:
我一直在研究各种选择.到目前为止,我提出的最好的(没有点#4 /#5)看起来有点像SCD type6-hybrid设置,但是没有当前布尔值,而是当前行的物化视图.出于所有意图和目的,它看起来像这样:
posts (
id pkey,
public,
created_at,
updated_at,
updated_by
)
post_revs (
id,
rev pkey,
public,
created_at,
created_by,
deleted_at
)
tags (
id pkey,
public,
created_at,
updated_at,
updated_by
)
tag_revs (
id,
public,
rev pkey,
created_at,
created_by,
deleted_at
)
post2tag (
post_id fkey posts(id),
tag_id fkey tags(id),
public,
created_at,
updated_at,
updated_by
)
post2tag_revs (
post_id,
tag_id,
post_rev fkey post_revs(rev), -- the rev when the relation started
tag_rev fkey tag_revs(rev), -- the rev when the relation started
public,
created_at,
created_by,
deleted_at,
pkey (post_rev, tag_rev)
)
Run Code Online (Sandbox Code Playgroud)
我正在使用pg_temporal来维护句点上的索引(created_at,deleted_at).并且我使用触发器使各个表保持同步.Yada yada yada ...我创建了触发器,允许取消对帖子/标签的编辑,使草稿存储到转速中而不发布.它很棒.
除非我需要担心post2tag上的草稿行相关关系.在那种情况下,所有的地狱都破裂了,这暗示着我在那里遇到了某种设计问题.但我的想法已经不多了......
我考虑过引入数据重复(即为每个草稿修订版引入了n post2tag行).这种作品,但往往比我想要的慢很多.
我考虑过为"最后草案"引入草稿表,但这很快就会变得非常丑陋.
我考虑过各种各样的旗帜......
所以问题:在行版本控制的环境中,是否有一种普遍接受的管理实时和非实时行的方法?如果没有,你尝试了什么,并取得了相当的成功?
Dam*_*vic 11
锚建模是实现时间dB的好方法 - 请参阅维基百科文章.需要一些时间来习惯,但工作得很好.有一个在线建模工具,如果你加载提供的XML文件,[File -> Load Model from Local File]
你应该看到这样的东西 - 也使用[Layout --> Togle Names]
.
该[Generate --> SQL Code]
会产生DDL表,视图和点的时间功能.代码很长,所以我不在这里发布.检查代码 - 您可能需要为数据库修改它.
这是要加载到建模工具的文件.
<schema>
<knot mnemonic="EXP" descriptor="Expired" identity="smallint" dataRange="char(1)">
<identity generator="true"/>
<layout x="713.96" y="511.22" fixed="true"/>
</knot>
<anchor mnemonic="US" descriptor="User" identity="int">
<identity generator="true"/>
<attribute mnemonic="USN" descriptor="UserName" dataRange="varchar(32)">
<layout x="923.38" y="206.54" fixed="true"/>
</attribute>
<layout x="891.00" y="242.00" fixed="true"/>
</anchor>
<anchor mnemonic="PO" descriptor="Post" identity="int">
<identity generator="true"/>
<attribute mnemonic="TIT" descriptor="Title" dataRange="varchar(2)">
<layout x="828.00" y="562.00" fixed="true"/>
</attribute>
<layout x="855.00" y="471.00" fixed="true"/>
</anchor>
<anchor mnemonic="TG" descriptor="Tag" identity="int">
<identity generator="true"/>
<attribute mnemonic="TGT" descriptor="TagText" dataRange="varchar(32)">
<layout x="551.26" y="331.69" fixed="true"/>
</attribute>
<layout x="637.29" y="263.43" fixed="true"/>
</anchor>
<anchor mnemonic="BO" descriptor="Body" identity="int">
<identity generator="true"/>
<attribute mnemonic="BOT" descriptor="BodyText" dataRange="varchar(max)">
<layout x="1161.00" y="491.00" fixed="true"/>
</attribute>
<layout x="1052.00" y="465.00" fixed="true"/>
</anchor>
<tie timeRange="datetime">
<anchorRole role="IsTagged" type="PO" identifier="true"/>
<anchorRole role="IsAttached" type="TG" identifier="true"/>
<anchorRole role="BYAuthor" type="US" identifier="false"/>
<knotRole role="Until" type="EXP" identifier="false"/>
<layout x="722.00" y="397.00" fixed="true"/>
</tie>
<tie timeRange="datetime">
<anchorRole role="Contains" type="PO" identifier="true"/>
<anchorRole role="ContainedIn" type="BO" identifier="false"/>
<layout x="975.00" y="576.00" fixed="true"/>
</tie>
<tie>
<anchorRole role="CreatedBy" type="TG" identifier="true"/>
<anchorRole role="Author" type="US" identifier="false"/>
<layout x="755.10" y="195.17" fixed="true"/>
</tie>
<tie>
<anchorRole role="CreatedBy" type="PO" identifier="true"/>
<anchorRole role="Author" type="US" identifier="false"/>
<layout x="890.69" y="369.09" fixed="true"/>
</tie>
<tie>
<anchorRole role="ModifiedBy" type="BO" identifier="true"/>
<anchorRole role="Author" type="US" identifier="false"/>
<layout x="1061.81" y="322.34" fixed="true"/>
</tie>
</schema>
Run Code Online (Sandbox Code Playgroud)
我想我成功了。基本上,您向相关表格添加一个(唯一的)草稿字段,然后您可以像处理新帖子/标签/等一样处理草稿:
posts (
id pkey,
public,
created_at stamptz,
updated_at stamptz,
updated_by int,
draft int fkey posts (id) unique
)
post_revs (
id,
public,
created_at,
created_by,
deleted_at,
pkey (id, created_at)
)
tags (
id pkey,
public,
created_at,
updated_at,
updated_by,
draft fkey tags (id) unique
)
tag_revs (
id,
public,
created_at,
created_by,
deleted_at,
pkey (id, created_at)
)
post2tag (
post_id fkey posts(id),
tag_id fkey tags(id),
public,
created_at,
updated_at,
updated_by,
pkey (post_id, tag_id)
)
post2tag_revs (
post_id,
tag_id,
public,
created_at,
created_by,
deleted_at,
pkey (post_id, tag_id, created_at)
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6379 次 |
最近记录: |