将数据从一个表更新到另一个表(在数据库中)

发布于 2024-08-12 23:05:18 字数 374 浏览 6 评论 0原文

数据库大师,

我希望有人能给我指明正确的方向。

我有两张桌子。表 A 和表 B。当系统启动时,表 A 中的所有条目都会被处理并复制到表 B(根据表 B 的架构)。表 A 可以有数万行。

当系统启动时,表 B 通过数据库更改通知与表 A 保持同步。

如果系统重新启动,或者我的服务重新启动,我想重新初始化表 B。但是,我想用尽可能少的数据库更新来完成此操作。具体来说,我想要:

  • 添加表 A 中但不在表 B 中的任何行,并
  • 删除表 A 中不存在但在表 B 中的任何行。
  • 表 A 和表 B 共有的任何行都应该是现在

,我不是一个“数据库人”,所以我想知道执行此操作的传统方法是什么。

DB gurus,

I am hoping someone can set set me on the right direction.

I have two tables. Table A and Table B. When the system comes up, all entries from Table A are massaged and copied over to Table B (according to Table B's schema). Table A can have tens of thousands of rows.

While the system is up, Table B is kept in sync with Table A via DB change notifications.

If the system is rebooted, or my service restarted, I want to re-initialize Table B. However, I want to do this with the least possible DB updates. Specifically, I want to:

  • add any rows that are in Table A, but not in Table B, and
  • delete any rows that are not in Table A, but are in Table B
  • any rows that are common to Table A and Table B should be left untouched

Now, I am not a "DB guy", so I am wondering what is conventional way of doing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

思念满溢 2024-08-19 23:05:18

使用 exists 将处理量保持在最低限度。

按照这些思路进行修改,以便连接正确(还要验证我没有做一些愚蠢的事情并从您的描述中向后获取 TableATableB):

insert into TableB
    select 
        *
    from
        TableA a
    where
        not exists (select 1 from TableB b where b.ID = a.ID)

delete from 
    TableB b
where
    not exists (select 1 from TableA a where a.ID = b.ID)

Use exists to keep processing to a minimum.

Something along these lines, modified so the joins are correct (also verify that I didn't do something stupid and get TableA and TableB backwards from your description):

insert into TableB
    select 
        *
    from
        TableA a
    where
        not exists (select 1 from TableB b where b.ID = a.ID)

delete from 
    TableB b
where
    not exists (select 1 from TableA a where a.ID = b.ID)
因为看清所以看轻 2024-08-19 23:05:18

Informix 的企业复制功能将为您完成这一切。 ER 的工作原理是将逻辑日志从一台服务器传送到另一台服务器,然后在辅助服务器上将它们前滚。

您可以根据需要将其配置为细粒度(即只有少数表)。

您使用术语“数据库更改通知” - 您是否已经在使用 ER 或者这是某种基于触发器的安排?

如果由于某种原因 ER 无法适合您的配置,我建议重写通知模型以异步运行,即:

  • 将通知写入服务器“A”中包含时间戳或序列字段的表,
  • 在服务器“B”上创建一个表' 存储最后处理的记录的时间戳/序列值,
  • 在服务器 'B' 上运行一个守护进程:
    • 比较“A”和“B”时间戳/序列
    • 选择“A”和“B”时间戳之间的“A”记录
    • 将这些记录处理为“B”
    • 更新“B”时间戳/序列号
    • 睡眠适当的时间段,然后循环

因此服务器“B”负责确保其副本与“A”同步。 “B”不可用并不会给“A”带来不便。

Informix's Enterprise Replication features would do all this for you. ER works by shipping the logical logs from one server to another, and rolling them forward on the secondary.

You can configure it to be as finely-grained as you need (ie just a handful of tables).

You use the term "DB change notifications" - are you already using ER or is this some trigger-based arrangement?

If for some reason ER can't work for your configuration, I would suggest rewriting the notifications model to behave asynchronously, ie:

  • write notifications to a table in server 'A' that contains a timestamp or serial field
  • create a table on server 'B' that stores the timestamp/serial value of the last processed record
  • run a daemon process on server 'B' that:
    • compares 'A' and 'B' timestamps/serials
    • selects 'A' records between 'A' and 'B' timestamps
    • processes those records into 'B'
    • update 'B' timestamp/serial
    • sleep for appropriate time-period, and loop

So Server 'B' is responsible for ensuring its copy is in sync with 'A'. 'A' is not inconvenienced by 'B' being unavailable.

一梦浮鱼 2024-08-19 23:05:18

一种简单的方法是使用历史表,您可以在其中放置自上次更新以来发生的 A 更改,并使用该表同步表 B,而不是从 A 直接复制到 B。同步完成后,您删除整个历史表并重新开始。

我不明白的是,如果您的服务或计算机没有运行,如何更新表 A 而不是表 B。它们是否在 2 个不同的数据库或服务器上找到?

A simple way would be to use a historic table where you would put the changes from A that happened since the last update, and use that table to sync the table B instead of a direct copy from A to B. Once the sync is done, you delete the whole historic table and start anew.

What I don't understand is how table A can be update and not B if your service or computer is not running. Are they found on 2 different database or server?

怪我入戏太深 2024-08-19 23:05:18

根据公共列连接两个表中的数据,这会给出两个表中都有匹配的行,即 A 和 B 中的数据。然后使用该值(我们称之为集合 M)进行集合操作,即集合减运算以获得差异。

第一个要求:A减去M
第二个要求:B减去A
第三个要求:M

你明白了吗?

Join data from both tables according to comon columns and this gives you the rows that have a match in both tables, i.e. data in A and in B. Then use this values (lets call this set M) with set operations, i.e. set minus operations to get the differences.

first requirement: A minus M
second requrement: B minus A
third requirement: M

Do you get the idea?

四叶草在未来唯美盛开 2024-08-19 23:05:18

我是一个 Sql Server 人员,但从 Sql Server 2008 开始,对于这种操作,可以使用功能调用 MERGE

通过使用 MERGE 语句,我们可以在单个语句中执行插入、更新和删除操作。

所以我用谷歌搜索,发现 Informix 也支持相同的 MERGE 语句,但我不确定它是否也处理删除,尽管正在插入和更新照顾好了。此外,该语句自行处理事务

I am a Sql Server guy but since Sql Server 2008, for this kind of operation , a feature call MERGE is available.

By using MERGE statement we can perform insert, update and delete operations in a single statement.

So I googled and found that Informix also supports the same MERGE statement but I am not sure whether it takes care of delete too or not though insert and update is being taken care off. Moreover, this statement takes care of transaction by itself

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文