jjzjj

mysql - RDBMS 建模 : Many to Many with Extreme Historical Versioning

coder 2023-10-24 原文

您有两个表,foobar,它们具有 M:N 关系。

您想维护 foobar 的相当极端的历史版本,以及它们之间的关系,例如:

  1. 你在 Foo 中插入一行,然后在 Bar 中插入一行,然后在 FooBar 中插入一行链接两人在一起。您应该能够及时回顾并看到 Foo 中的行曾经是独立的,Bar 中的行也是如此。

  2. 然后,您将另一行插入到 Bar 中,并在 FooBar 中插入一行,将第二个 bar 链接到第一个 >富。您应该能够及时回顾并看到 foo 行仅链接到第一个 bar 行。

  3. 然后您更新 foo 行的属性之一。您应该能够及时回顾并看到 Bar 中的两行都曾经链接到具有前一个属性的 foo 行。

虽然我能够实现这一点,但我的解决方案相当乏味,并且会导致针对单个更新/插入的大量 DML 操作。在 Bar 和 Baz 之间添加一个带有 M:N 的 Baz 表会显着增加 DML 的数量。是否有比以下方法更好地完成此任务的标准方法?

这是我的解决方案:


DDL

Foo
--------------
foo_id            INT            --sequence generated
foo_version_id    INT     UNIQUE --sequence generated
foo_name          VARCHAR
active            INT     CHECK (active in (0,1))
CONSTRAINT  PRIMARY_KEY (foo_id, foo_version_id)


Bar
--------------
bar_id            INT            --sequence generated
bar_version_id    INT     UNIQUE --sequence generated
bar_name          VARCHAR
active            INT     CHECK (active in (0,1))
CONSTRAINT  PRIMARY_KEY (bar_id, bar_version_id)

FooBar
--------------
foo_version_id    FK to foo.foo_version_id
bar_version_id    FK to bar.bar_version_id
CONSTRAINT PRIMARY KEY (foo_version_id, bar_version_id)

DML

以下是三种情况的伪代码。我已将这些作为程序实现。

对于案例#1,这导致 4 个 DML 操作将两个独立的 foobar 链接在一起,不包括前两个行:

Insert the first foo row
Insert the first bar row
Update the first foo row and set active to 0. 
Insert a new foo row with the same foo_id, foo_name, but new foo_version_id
Update the first bar row and set active to 0
Insert a new bar row and with the same bar_id, bar_name, but new bar_version_id
Insert a row into foo_bar with the foo_version_id and bar_version_id from the newly created active foo and bar rows.

对于案例 #2,这会导致 9 个 DML 操作将新的 bar 链接到 foo,而 foo 链接到第一个 bar,不包括第一行:

Insert the second bar row 
Update the active foo and set active to 0
Insert a new foo row with same foo_id, foo_name, but new foo_version_id
Update the first active bar and set active to 0
Insert a new bar row with same bar_id, bar_name, but new bar_version_id
Update the second active bar and set active to 0
Insert a new bar row with same bar_id, bar_name, but new bar_version_id
Insert a row into foo_bar with the foo_version_id and bar_version_id from the foo and first bar.
Insert a row into foo_bar with the foo_version_id and bar_version_id from the foo and second bar.

对于案例 #3,这会导致 8 个 DML 操作来更新链接到两个 barsfoo 上的属性:

Update the active foo and set active to 0
Insert a new foo row with same foo_id, but new foo_version_id, foo_name
(repeat from case #2 starting at line 4)

SQL

给定一个已知的 foo_id,我可以在 foo_version_id 上加入 foofoo_barbar bar_version_id 并查看所讨论的特定 foo 的所有可能的历史状态。

select f.foo_id, f.foo_version_id, f.foo_name, b.bar_id, b.bar_version_id, b.bar_name
FROM foo f, foo_bar fb, bar b
WHERE 1 = 1
    AND f.foo_version_id = fb.foo_version_id (+)
    AND fb.bar_version_id = b.bar_version_id (+)
ORDER BY f.foo_version_id, b.bar_version_id
;

foo_id | foo_version_id | foo_name | bar_id | bar_version_id | bar_name
     1 |              1 |        a |        |                |           -- 1) independent foo
     1 |              2 |        a |      1 |              2 |       b   -- 2) link foo to first bar
     1 |              3 |        a |      1 |              4 |       b   -- 3) link second bar to foo
     1 |              3 |        a |      2 |              5 |       b2  -- 3) link second bar to foo
     1 |              4 |        A |      1 |              6 |       b   -- 4) rename foo_name to A
     1 |              4 |        A |      2 |              7 |       b2  -- 4) rename foo_name to A

最佳答案

您需要一个时态数据库,即支持 SQL:2011 Temporal 的数据库(或类似的专有系统)

据我所知,没有开源数据库支持这一点。一段时间以来,我一直在缠着 Posgres 的人去做这件事。

这意味着您需要支付一些现金。以下数据库支持它:

IBM DB2 10+
甲骨文 12c
微软 SQL Server 2016

关于mysql - RDBMS 建模 : Many to Many with Extreme Historical Versioning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35399312/

有关mysql - RDBMS 建模 : Many to Many with Extreme Historical Versioning的更多相关文章

  1. ruby-on-rails - 建模收藏夹 - 2

    我希望将Favorite模型添加到我的User和Link模型。业务逻辑用户可以有多个链接(即可以添加多个链接)用户可以收藏多个链接(他们自己的或其他用户的)一个链接可以被多个用户收藏,但只有一个所有者我对如何为这种关联建模以及在模型就位后如何创建用户收藏夹感到困惑?classUser 最佳答案 下面的数据模型怎么样:classUser:destroyhas_many:favorite_links,:through=>:favorites,:source=>:linkendclassLink:destroyhas_many:favor

  2. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  3. ruby-on-rails - 无法安装 mysql2 0.3.14 gem - 2

    我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby​​目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin

  4. ruby - 如何使用 ruby​​ mysql2 执行事务 - 2

    我已经开始使用mysql2gem。我试图弄清楚一些基本的事情——其中之一是如何明确地执行事务(对于批处理操作,比如多个INSERT/UPDATE查询)。在旧的ruby-mysql中,这是我的方法:client=Mysql.real_connect(...)inserts=["INSERTINTO...","UPDATE..WHEREid=..",#etc]client.autocommit(false)inserts.eachdo|ins|beginclient.query(ins)rescue#handleerrorsorabortentirelyendendclient.commi

  5. 建模分析 | 平面2R机器人(二连杆)运动学与动力学建模(附Matlab仿真) - 2

    目录0专栏介绍1平面2R机器人概述2运动学建模2.1正运动学模型2.2逆运动学模型2.3机器人运动学仿真3动力学建模3.1计算动能3.2势能计算与动力学方程3.3动力学仿真0专栏介绍?附C++/Python/Matlab全套代码?课程设计、毕业设计、创新竞赛必备!详细介绍全局规划(图搜索、采样法、智能算法等);局部规划(DWA、APF等);曲线优化(贝塞尔曲线、B样条曲线等)。?详情:图解自动驾驶中的运动规划(MotionPlanning),附几十种规划算法1平面2R机器人概述如图1所示为本文的研究本体——平面2R机器人。对参数进行如下定义:机器人广义坐标

  6. ruby-on-rails - 当我通过 rvm 使用 rails3 时,如何在 ubuntu 上安装 mysql2 gem? - 2

    我正在尝试绕过rails配置这个极其复杂的迷宫。到目前为止,我设法在ubuntu上设置了rvm(出于某种原因,ruby在ubuntu存储库中已经过时了)。我设法建立了一个Rails项目。我希望我的测试项目使用mysql而不是mysqlite。当我尝试“rakedb:migrate”时,出现错误:“!!!缺少mysql2gem。将其添加到您的Gemfile:gem'mysql2'”当我尝试“geminstallmysql”时,出现错误,告诉我需要为安装命令提供参数。但是,参数列表很大,我不知道该选择哪些。如何通过在ubuntu上运行的rvm和mysql获取rails3?谢谢。

  7. ruby-on-rails - 如何针对组合字段的唯一性对这种复杂的验证进行建模 - 2

    link有两个组件:componenta_id和componentb_id。为此,在Link模型文件中我有:belongs_to:componenta,class_name:"Component"belongs_to:componentb,class_name:"Component"validates:componenta_id,presence:truevalidates:componentb_id,presence:truevalidates:componenta_id,uniqueness:{scope::componentb_id}validates:componentb_id

  8. Centos7-yum安装mysql-修改密码-无密码登录-安全配置 - 2

    目录1、yum安装mysql修改密码(1)在mysql里面修改(2)第二种方式,利用mysqladmin修改密码2、没有密码,登录mysql修改密码3、mysql的安全设置1、yum安装mysql在CentOS中默认安装有MariaDB(MySQL的一个分支),安装完成之后可以直接覆盖MariaDB。rpm-qa|grepmariadb查询是否安装了mariadbrpm-e--nodepsmariadb-libs-5.5.60-1.el7_5.x86_64卸载mariadwgethttp://dev.mysql.com/get/mysql57-community-release-el7-11.

  9. 设计一个亿级高并发系统架构 - 12306火车票核心场景DDD领域建模 - 2

    “架设一个亿级高并发系统,是多数程序员、架构师的工作目标。许多的技术从业人员甚至有时会降薪去寻找这样的机会。但并不是所有人都有机会主导,甚至参与这样一个系统。今天我们用12306火车票购票这样一个业务场景来做DDD领域建模。”开篇要实现软件设计、软件开发在一个统一的思想、统一的节奏下进行,就应该有一个轻量级的框架对开发过程与代码编写做一定的约束。虽然DDD是一个软件开发的方法,而不是具体的技术或框架,但拥有一个轻量级的框架仍然是必要的,为了开发一个支持DDD的框架,首先需要理解DDD的基本概念和核心的组件。一.什么是领域驱动设计(DDD)首先要知道DDD是一种开发理念,核心是维护一个反应领域概

  10. ruby - 安装 dm-mysql-adapter 时出错 - 2

    我是Ruby的新手。我安装了DataMapper并且正在尝试安装dm-mysql-adapter-1.0.2gem。但是当我尝试安装时,出现以下错误。我正在使用ubuntu操作系统。vinoth@vinoth-laptop:~/Downloads$geminstalldm-mysql-adapter-1.0.2----with-mysql-lib=/usr/lib/mysql----with-mysql-conf=/usr/bin/mysqlWARNING:Installingto~/.gemsince/home/vinoth/gemsand/home/vinoth/gems/bina

随机推荐